Jump to content

[SOLVED] Time out?


AV1611

Recommended Posts

I have a website that pulls some data from another site with file_get_contents();

 

It works great ... most of the time... but when the site I'm pulling from goes down (rarely) then my script hangs for a very long time...

 

Is there a way for me to put ...say... a 10 second time limit on the file_get_contents() so it's just returns null or error or whatever then the rest of the page will load like normal?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/138055-solved-time-out/
Share on other sites

<?php
$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 1
        )
    )
);
file_get_contents("http://example.com/", 0, $ctx);
?>

 

Found that at file_get_contents in the user's remarks. Not sure if that will work, but yea.

Link to comment
https://forums.phpfreaks.com/topic/138055-solved-time-out/#findComment-721595
Share on other sites

ok, that works good... now...

 

Here is the error I get when the link is down:

 

Warning: file_get_contents(http://DOMAIN.com/aaoxml/PortalClanStats.php) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/web2/web/indexnew.php on line 488

 

What I need now is instead of the error I get for it to just give me 'Link Down' if it's is "false"...

 

Help?

Link to comment
https://forums.phpfreaks.com/topic/138055-solved-time-out/#findComment-721609
Share on other sites

<?php
$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 1
        )
    )
);
if(false === $str = @file_get_contents("http://example.com/", 0, $ctx)){echo 'Link Down';} else {echo $str;}
?>


Well not sure if that's the best way to do it but it seems to work? I'll know for sure when the link comes back up 

Link to comment
https://forums.phpfreaks.com/topic/138055-solved-time-out/#findComment-721624
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.