gpt4 book ai didi

php - 本地主机(在自定义端口)在 POST 请求后返回空数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:12 26 4
gpt4 key购买 nike

我正在尝试使用 PHP 从本地服务器 localhost 读取数据,但它似乎没有返回任何数据。这是我的完整脚本。

<?php
$address = "localhost";
echo "Attempting to open the socket at ".$address."...\n";
$fp = fsockopen($address, 49801, $errno, $errstr, 10);
echo "Error number is "; echo $errno; echo ".\n";
echo "Error string is ".$errstr.".\n";
echo "Attempt complete.\n";
if ($fp) {
print "Socket in now open.\n";
syslog(LOG_INFO, "socket.php: Reaching the specified address...");
print "Writing requests...\n"; //Hangs for about 1-2 minutes
fwrite($fp, "POST / HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\nHost: ".$address."Content-Type: text/xml\r\nContent-Length: ".strlen($payload)."\r\n\r\n");
$msg = "";
while($data = fread($fp, 32768)) {
$msg= $msg.$data;
}
if (strlen($msg) != 0) {
print "Final message: ***".$msg."***\n";
} else {
print "There is no data received from '".$address."'\n";
}
fclose($fp);
} else {
print "Error\n";
}
?>

这是我在终端中得到的输出:

Attempting to open the socket at localhost...
Error number is 0.
Error string is .
Attempt complete.
Socket in now open.
Writing requests...
There is no data received from 'localhost'

如上面脚本中所述,倒数第二行 Writing requests... 挂起大约 1 或 2 分钟,然后附加一个空字符串。

我觉得这很好奇,因为这个脚本在 HTTP 的端口 80 或 SSH 的端口 22 上运行良好。我限制了对 localhost:49801 的配置的访问,因此无法进行服务器配置的任何更改。

然而,我想知道服务器的配置是否有问题,这样我就不用再费心了。

顺便说一句,我在 CentOS 7 上运行 PHP 5.4。

编辑
“Content-Length: 111”中的“111”是一个任意数字,取决于负载的字符串长度。

感谢您的帮助!

最佳答案

你没有得到服务器的回复,因为他正在等待你的 111 字节数据。

如果您向您的服务器发送正确数量的数据,他将做出相应的响应。

下面是我将 Content-Length 更改为 9

的工作示例

然后我使用 fwrite 发送 9 位数据:

<?php
$address = "localhost";
$payload = "Some data";
echo "Attempting to open the socket at ".$address."...\n";
$fp = fsockopen($address, 49801, $errno, $errstr, 10);
echo "Error number is "; echo $errno; echo ".\n";
echo "Error string is ".$errstr.".\n";
echo "Attempt complete.\n";
if ($fp) {
print "Socket in now open.\n";
syslog(LOG_INFO, "socket.php: Reaching the specified address...");
print "Writing requests...\n";

fwrite($fp, "POST / HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\nHost: ".$address.
"\r\nContent-Type: text/xml\r\nContent-Length: ". strlen($payload) ."\r\n\r\n");
//We send the data to the server
fwrite($fp, $payload);

$msg = "";
while($data = fread($fp, 32768)) {
$msg= $msg.$data;
}
if (strlen($msg) != 0) {
print "Final message: ***".$msg."***\n";
} else {
print "There is no data received from '".$address."'\n";
}
fclose($fp);
} else {
print "Error\n";
}
?>

关于php - 本地主机(在自定义端口)在 POST 请求后返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37145357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com