fopen fails randomly

Discussion in 'PHP' started by benjiw, Mar 2, 2011.

  1. #1
    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&amp;f=sl1d1t1c1ohgv&amp;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.
     
    benjiw, Mar 2, 2011 IP
  2. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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):
     
    MartinPrestovic, Mar 2, 2011 IP
  3. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Mike Griffiths, Mar 4, 2011 IP
  4. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    'HTTP request failed!'


    Contact Yahoo Support.
     
    eleetgeek, Mar 5, 2011 IP