I am accessing an XML API (flight search comparison) via a PHP Proxy like here.
The API always has a full file for download. The first version states there are 0 results, then after calling it a few times the first results pop up.
API documentation says:
the result method will always give you the current state of the
searchresult. You may call it multiple times (maybe with ajax calls)
My problem is, I don’t know when it’s done, i.e. when all results are delivered.
I tried this:
-
read till
feof, then reset the file position indicator viafseek()and wait a little like here but that doesn’t work, as the stream is not supportingfseek(). -
waiting a few seconds and then displaying the results, but that doesn’t work well as the API sometimes needs very long (and I am not getting everything)
-
read till
feof, then usingftell()to see whether new stuff has been written to the file, but I am lacking the possibility to do something afterwards …$handle = fopen($flights_url, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); echo $buffer; } fclose($handle); }
I am out of ideas now. How should this be done in a nice way?
-T
Edit: changed formatting and added some code
Okay, I contacted one of the API’s developers.
He told me, that they don’t send a ready-signal, because they also don’t know, when the response is finished. This is because they work with so many other APIs (it’s a comparison API, after all).
The advice/solution he gave me, and which they use, is to poll a few times, with a few seconds delay between each poll, and then just take what’s there and leave the rest.
Seems rather pragmatic than elegant to me, but that’s how they do it 🙂 Thanks to all who tried to give input!