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.
The snippet has several problems:
strcmp($r, "")
doesn't actually make sense. It works, but it doesn't make sense. I don't think fgets
ever returns an empty string. If there is no more data available, it returns FALSE
. The reason your comparison works is that FALSE
is converted to an empty string. But it makes your code not clear.fgets
returns false, you're potentially reading only the first batch of data that was cached by the OS. You try to work around this by forcing a wait no data has been received yet, but it's a brittle solution.The two last points can be solved by using stream_select
. This way you can enforce a large timeout until the first packet is received and then use a small timeout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With