Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.