Time out for fopen

Discussion in 'PHP' started by redhits, Sep 18, 2007.

  1. #1
    Can i set an time out for a fopen function ?
    I wan't to open websites but to not wait for 30 seconds if the websites are down , only 2-3 seconds. How can i do it without curl?!
     
    redhits, Sep 18, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use fsockopen(), which takes a timeout parameter. Then call set_stream_timeout(), which will set the timeout for subsequent read operations.

    See the fsockopen manual page for more details: http://us3.php.net/manual/en/function.fsockopen.php
     
    sea otter, Sep 18, 2007 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Also try file_get_contents and see if it works better. Sometimes other sites are just too slow for either to work effectively.
     
    jestep, Sep 18, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, you *could*, but the only way to control the timeout would be to create a stream context, set the timeout on that (as previously described), and then pass that context in to file_get_contents.

    However, stream contexts with file_get_contents can be persnickety, so this isn't really recommended.
     
    sea otter, Sep 18, 2007 IP