Background: I have to create a plain site that accepts incoming posted XML and sends the XML to a server via a socket connection and in turn display the XML sent back from the server. Easy peasy.
Problem: I had no problem utilising fsockopen() to connect to the server and sending the XML. Reading the XML from the server was a whole new problem. The normal
while (!feof($fp)) {
echo fgets($fp);
}
did not do the trick, since the server returns one XML string, and one XML string only (no length information, eof, eol, etc.). Thus it would wait until the timeout was hit, display the received XML and a timeout error. My problem is similar to this dinosaur.
In a nutshell I want to read XML on the socket and close it as soon as no more data is being sent (not wait for timeout). Setting the timeout to a low value was also not viable as the server response can vary between 2--30 seconds.
Solution: After struggling for the entire afternoon I decided to share the following solution for this problem (do criticise).
$fp = fsockopen("123.456.789.1", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)";
} else {
$wait = true;
$out = '<samplexml><item>something</item></samplexml>';
// [1] disable blocking
stream_set_blocking($fp, 0);
fwrite($fp, $out);
while (!feof($fp)) {
$r = fgets($fp);
echo $r;
if (!strcmp($r, "")){
if (!$wait) {
// [2] has recieved data on this socket before
break;
}
} else {
$wait = false;
}
}
fclose($fp);
}
It turns out that my main problem was blocking. So first off, [1] I had to disable stream_set_blocking so that fgets() can continuously check if new data became available. If not disabled fgets() will get the XML from the server and then the loop will get stuck on the second try since it will wait until more data becomes available (which never will).
I know that as soon as we have read some data that we can immediatly close the connection if any empty fgets() are returned (thus we can still set the second parameter of fgets() if it should be necessary).
After using this site for months I finally got to post something on stackoverflow.