2

谁能帮我解决本地主机中的 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 中解决这个问题。先感谢您。

4

1 回答 1

3

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 );  
?>
于 2011-12-08T05:25:47.117 回答