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!