Jump to content

Can you set a timeout for your script


Presto-X

Recommended Posts

Hey guys,

 

I'm working on a script that pulls my latest tweet from twitter, the script seems to work really well, the only problem that I found, is when twitter is overloaded the script times out, but you have to wait like 30 sec. for the page to load to tell you that.  Is there away to set a timeout of like 2-3 sec. then if its not done kick out a nice error message, or better yet read from a local cache file?

 

Here is my current script:

 

function getTwitterStatus($userid){
$url = 'https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=' . $userid . '&count=1';
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
	$tweet = array( 'created' => how_long_ago(strtotime($status->created_at)), 'text' => $status->text, 'username' => $status->user->screen_name);
}
return $tweet;
}

Link to comment
https://forums.phpfreaks.com/topic/264566-can-you-set-a-timeout-for-your-script/
Share on other sites

you can do it via javascript.

 

<div id="twitter_update_list"><!-- Your Latest Tweet Will Display Here Once Loaded --></div>
<script type="text/javascript" src="//apis.jssnips.com/twitterblogger/latest.js"></script>
<script type="text/javascript" src="//twitter.com/statuses/user_timeline/my_user_name.json?callback=twitterCallback2&count=1"></script>

 

replace my_user_name with your twitter username

Thanks for the replies,

 

So with PHP would I do something like:

 

function getTwitterStatus($userid){
set_time_limit(2);
$url = 'https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=' . $userid . '&count=1';
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
	$tweet = array( 'created' => how_long_ago(strtotime($status->created_at)), 'text' => $status->text, 'username' => $status->user->screen_name);
}
return $tweet;
}

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.