3

I am trying to use a PHP to read data from the local server localhost, but it seems no data is returned from it. Here is my full script.

<?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";
  }
?>

Here is the output I am getting in the terminal:

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'

As mentioned in the script above, the second last line Writing requests... hangs for about 1 or 2 minutes, then an empty string is appended.

I think it is rather curious because this script works well on HTTP's port 80 or on SSH's port 22. I have restricted access to localhost:49801's configuration, and thus am not able to make any changes to the server's config.

I was however wondering if something was wrong with the server's config so that I don't have to tear out my hair for another day.

By the way, I am running PHP 5.4 on CentOS 7.

Edit
The '111' in "Content-Length: 111" is an arbitrary number that depends on the payload's string length.

Thanks your your help!

  • Why not just use standard HTTP methods instead of working with C socket APIs? – miken32 May 10 '16 at 21:53
  • Also, you're telling the HTTP server to expect a 111 byte body but then not sending it. – miken32 May 10 '16 at 21:54
  • Hmm, I am not sure I understand what you mean by "C socket API", also I forgot to mention that the 111 is just an arbitrary number that varies depending on the payload. I'll update the post. – The Drummer from Kubuntu May 11 '16 at 00:49
  • If you search for how to post HTTP from PHP, I can assure you almost none of your results will involve `fsockopen()`. Try using curl or even just `file_put_contents()` with a steam. – miken32 May 11 '16 at 00:51
  • And your edit doesn't change that you aren't sending anything after the headers. – miken32 May 11 '16 at 00:52
  • I did not consider that idea, I'll do it first thing in the morning I get to work, thanks! – The Drummer from Kubuntu May 11 '16 at 00:52
  • See here for details: http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php – miken32 May 11 '16 at 17:02

1 Answers1

0

You got no reply from your server because he is waiting 111 bytes of data from you.

if you send the right amount of data to your server he will respond accordingly.

Below is working example where I change the Content-Length to 9

and then I send the 9 bites of data using fwrite:

<?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";
  }
?>
Angel115
  • 2,135
  • 1
  • 17
  • 20
  • Thank you for your answer. I just tried this piece of code in the terminal, but when run the actual app (it has the same code, but instead of being a number like 111 or 9 in your example, it is strlen($payload), which is always different than 0), it doesn't :D. Do you think the server is expecting a certain size of data and just not responding? If so, do you thing it's possible to find a way of reading the size of data the server's expecting? Thanks – The Drummer from Kubuntu May 11 '16 at 17:24
  • Oh gosh my brain was very slow, I understand that strlen("some data") = 9, so yes... but I find it rather odd that it doesn't work with strlen($payload) which is exactly what I said in my previous comment. I think the issue is on the server side. If so, I'll accept your answer. – The Drummer from Kubuntu May 11 '16 at 19:02
  • I just change my answer above to reflect your comment. When I try it, it's working so I don't really understand what is your problem. Can you please tell me if the example above is working for you? If not I'll need the exact same code that your are working on to see what's wrong. – Angel115 May 12 '16 at 05:58
  • Yes, thank you, I forgot to say the code works fine on the terminal, but not on the actual app, but the app was hanging because of a Python issue.... -_- – The Drummer from Kubuntu May 12 '16 at 15:10