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

Php 本地主机(在自定义端口)在POST请求后返回空数据,php,post,localhost,Php,Post,Localhost,我试图使用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 ".\

我试图使用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";
  }
?>
如上面的脚本所述,最后一行
Writing requests…
挂起约1或2分钟,然后追加一个空字符串

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

然而,我想知道服务器的配置是否有问题,这样我就不必再为下一天而烦恼了

顺便说一下,我正在CentOS 7上运行PHP5.4

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


谢谢你的帮助

您的服务器没有回复,因为他正在等待您发送的111字节数据

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

下面是我将内容长度更改为9的工作示例

然后我使用写入发送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";
  }
?>


为什么不使用标准的HTTP方法而不是使用C套接字API?另外,您告诉HTTP服务器希望有111字节的正文,但不发送它。嗯,我不确定我是否理解您所说的“C套接字API”是什么意思,而且我忘了提到111只是一个任意数字,根据负载的不同而有所不同。我将更新帖子。如果您搜索如何从PHP发布HTTP,我可以向您保证,您的结果几乎不会涉及
fsockopen()
。尝试使用curl,甚至只使用steam
file\u put\u contents()
。您的编辑不会改变您在标题后不发送任何内容。谢谢您的回答。我刚刚在终端中尝试了这段代码,但当运行实际的应用程序时(它有相同的代码,但在您的示例中不是像111或9这样的数字,而是strlen($payload),它总是不同于0),它不是:D。您认为服务器是否期望一定大小的数据,只是没有响应?如果是这样的话,您是否认为有可能找到一种读取服务器期望的数据大小的方法?谢天谢地,我的大脑非常慢,我知道strlen(“一些数据”)=9,所以是的。。。但我觉得很奇怪,它与strlen($payload)不起作用,这正是我在之前的评论中所说的。我认为问题出在服务器端。如果是的话,我会接受你的回答。我只是改变我的回答以反映你的评论。当我尝试它时,它起作用了,所以我真的不明白你的问题是什么。你能告诉我上面的例子是否适合你吗?如果没有,我将需要与您正在处理的代码完全相同的代码来查看问题所在。是的,谢谢,我忘了说代码在终端上工作正常,但在实际应用程序上没有,但应用程序由于Python问题而挂起-_-
<?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";
  }
?>