本地主机(在自定义端口)returns POST 请求后的空数据
localhost (at custom port) returns empty data after POST request
我正在尝试使用 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
的配置,因此无法对服务器的配置。
不过我想知道是不是服务器的配置有问题,这样我就不用再折腾一天了。
顺便说一句,我是 运行 PHP CentOS 7 上的 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 从本地服务器 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
的配置,因此无法对服务器的配置。
不过我想知道是不是服务器的配置有问题,这样我就不用再折腾一天了。
顺便说一句,我是 运行 PHP CentOS 7 上的 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";
}
?>