fopen versus sockets & timeouts

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here's what I'm trying to do. The server serves up XML documents based
on what I specify in the GET string. This is on a server I have no
control over.

There is a lag between the time when I issue the  GET request, and when
I actually get the response. (I suspect because the server has to dig
for the data and format it. In any case, it's longer than say, a simple
HTML page request to the web server.)

When I manually cut and paste the GET url into a browser, it sometimes
takes up to 20 seconds to get the response back (the popup tell me to
save the file).

When I fopen() it, sometimes it works, sometimes it doesn't. I tried
using set_time_limit(0), but that doesn't affect it.

I believe I might have to do a socket call in order to have more fine
tune control over the HTTP request.

Am I heading down the right path?

Re: fopen versus sockets & timeouts

Quoted text here. Click to load it

No. Check your stream timeout settings instead.

streams & file_get_contents()? can't set timeout?


Thanks for the suggestion - I tried setting the socket timeout with
stream_set_timeout() and it seems to have fixed the problem:

function get_fopen_contents($url)
    if (!$handle = fopen("$url", "r")) return -1;
    stream_set_timeout($handle, 1000);
    while (!feof($handle))
      $contents .= fread($handle, 8192);
    if (fwrite($handle, $contents) === FALSE) {
           echo "Cannot write to file ($filename)";

    return $contents;

However, I didn't realize there already was a file_get_contents() so I'm
using that.

Now, I guess by using file_get_contents() I have no control over the
timeout, because I need to refer to the handle of the stream in some
way. Right?

Quoted text here. Click to load it

Site Timeline