I am having an issue in which fopen randomly generates either timeout or "failed to open stream" errors when trying to get stock quotes from yahoo finance. Here's an example. <b>Warning</b>: fopen(http://finance.yahoo.com/d/quotes.csv?s=NFLX&f=sl1d1t1c1ohgv&e=.csv) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: HTTP request failed! This issue occurs sporadically; sometimes it's fine, other times it errors out causing the quote to fail. Any ideas on how I can resolve this? Thanks very much.
It's hard to give you an exact example without seeing your code, but what I would do is add a loop. So for example say you are assigning the result of the fopen to $var1, I would stick that in a while loop so that it keeps retrying the fopen request until the data you are looking for is returned. I would also add in some limits to it, to prevent it from overloading. If Yahoo goes down for example your while would be in a never ending loop so you would want to add in a break out after 30 attempts. Also, you can add the @ character directly in front of your fopen request to supress errors. So if there is an error it will not stop the script.... e.g $var1 = @fopen(); Code (markup):
I'd guess it's a problem with an unstable Internet connection. Code very rarely works or doesn't work, it's normally one or the other. Perhaps Yahoo is throttling you? Try using cURL instead, you'll get a better idea of what is happening.