任何人都可以帮我解决本地主机中的 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 中收到无法解析地址错误。 谁能解释一下如何在本地主机中解决这个问题。 预先感谢您。
最佳答案
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 );
?>
关于本地主机上的 fsockopen 演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426191/