在localhost上的fsockopen演示

时间:2011-12-08 04:32:24

标签: fsockopen demo

任何人都可以帮我解决localhost中的fsockopen问题。

我创建了fsock.php,将变量发布到同一文件夹中的test121.php。 http://localhost/ftp/fsock.php

<?php
$fp = fsockopen('localhost/ftp');

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost/ftp \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}?>

这是test121.php中的代码 http://localhost/ftp/test121.php

<?php
echo "<br><br>";    
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];  
 parse_str( $raw_data, $_POST );

//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );  
?>

我在fsock.php中遇到无法解析地址错误。 任何人都可以解释我如何在localhost中解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

fsock.php     

$fp = fsockopen("localhost", 80, $errno, $errstr, 30);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST http://localhost/ftp/test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}?>

您可能还想更改test121.php并删除$ GLOBALS(不确定为什么要使用它):

<?php
echo "<br><br>";    
$raw_data = $_POST;

//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );  
?>