PHP's file_get_contents() and an Atom Feed
-
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 http://ask.metafilter.com/156368/Help-Me-Build-a-Custom-FriendFeed, which is working great, except for this one issue.)
-
Answer:
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.
JPowers at Ask.Metafilter.Com Visit the source
Other answers
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://www.php.net/manual/en/context.http.php that you can pass to your file_get_contents call
gregjones
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
missmagenta
I believe the problem is that you're not actually getting a file, you should probably use curl and enable redirects.
wongcorgi
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);
missmagenta
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?
artlung
Related Q & A:
- What's wrong with this PHP Twitter API POST?Best solution by Stack Overflow
- How to store data in php and get data from php?Best solution by Stack Overflow
- The difference between an atom and an element is?Best solution by Yahoo! Answers
- Which of the following atom is the most paramagnetic and why?Best solution by Yahoo! Answers
- How do you determine the number of neutrons in an atom?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.