PHP's file_get_contents() and an Atom Feed
July 17, 2010 1:13 PM   Subscribe

Help me figure out why an ATOM feed won't play nice with PHP's file_get_contents() method.

Here's my code:

$google_reader_feed = file_get_contents("http://www.google.com/reader/public/atom/user/00703537031505701317/state/com.google/broadcast");

About 65% of the time this method returns the following error:

failed to open stream: Connection timed out

I've used this function on plenty of other pages and it works fine. Only difference (that I can see) between those pages and this one is that they are RSS feeds and this is an ATOM feed. I realize both are XML.

Any ideas?

(FWIW: I'm implementing sandig's idea here, which is working great, except for this one issue.)
posted by JPowers to Computers & Internet (6 answers total)
 
You might try upping the default_socket_timeout value in php.ini, although I have a feeling that is a read timeout and not a connect timeout, with the latter being a function of the underlying operating system's TCP/IP stack settings and not of the application. If PHP is really failing to connect then I don't see much that you can do about that other than investigate why connectivity is so poor or add some retries.
posted by Rhomboid at 1:44 PM on July 17, 2010


The format of the file is irrelevant. Is this feed bigger than your other ones? Are the others also coming from google.com? The setting that controls timeout for reading http streams is 'default_socket_timeout ' in your php.ini, or you can set a 'timeout' value in the options of a http stream-context that you can pass to your file_get_contents call
posted by gregjones at 1:47 PM on July 17, 2010


You say the method is returning the error - do you mean $google_reader_feed contains "failed to open stream: Connection timed out" - in my experience file_get_contents will wait until the script timeout limit, I had a client that was killing his server because he used file_get_contents on a stream that took over 60s to return a result a lot of the time (and the script was on every page!) so I believe that the message you're getting is actually the result of file_get_contents (in that google read is saying it timed out not the function itself)

How long does it take to get this result? I would switch your file_get_contents for a very simple curl exec (assuming your server supports it) which will give you a lot more control. You should use curl in preference to file_get_contents for external resources whenever possible - with strict timeouts so they don't eat all your server resources - but that probably not the problem here.

How often are you running the script? Google may be rate limiting requests
posted by missmagenta at 1:52 PM on July 17, 2010


I believe the problem is that you're not actually getting a file, you should probably use curl and enable redirects.
posted by wongcorgi at 3:00 PM on July 17, 2010


On preview Rhomboid and gregjones are probably right about your default_socket_timeout being too low - I've never experienced that myself as the default is 60s (same as the default script timeout) and I've never had any clients with cause to change it - I'm guessing if yours is lower than that you're on shared hosting (or you're increasing the script timeout limit)?

But unless your default_socket_timeout is ludicrously low, its only the reason for the error message not the cause of the problem. Even its only 5-10s thats long enough that you shouldn't be failing to connect 65% of the time but thats unlikely to be a problem you can fix within your php script. If you have it installed I would give cURL a try

$ch = curl_init(http://www.google.com/reader/public/atom/user/00703537031505701317/state/com.google/broadcast');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if($data = curl_exec($ch))
{
echo $data;
}
else
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
posted by missmagenta at 3:20 PM on July 17, 2010


You need to be caching the results of that and not hitting that feed so often.

What do your server error logs (php error logs specifically) indicate?
posted by artlung at 4:44 PM on July 20, 2010


« Older How long for shipping for an iPhone 4?   |   Vegan marshmallows in Montreal? Newer »
This thread is closed to new comments.