views:

238

answers:

2

Hello all,

I make use of a great class made by Jaisen: http://github.com/jmathai/twitter-async/tree/master. Recently twitter has been up and down and I am sure it will continue to be the same in the future so I am trying to cut down on my dependency on twitter being actually working.

The following is what I have in my header.php and it is right at the top and it generates the login URL for each user. If twitter is down, my site hangs for as long as it needs to and it throws an exception. So I have to catch these expceptions which I have sort of done.

I now want to just cancel the request to the API after a few seconds and just load the page and keep trying behind the scenes. How can I best do this?

<?php include './twitter/EpiCurl.php'; include './twitter/EpiOAuth.php'; include './twitter/EpiTwitter.php';
$consumer_key = 'mykey';
$consumer_secret = 'mysecret';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret); 

try{  

$twiturl = $twitterObj->getAuthenticateUrl();
$url = "window.open('".$twiturl."','Login', 'left=20,top=20,width=500,height=500,toolbar=0,resizable=1'); startLoad();";

}catch(EpiOAuthBadRequestException $e){  
  // bad request exception do something  
  $statusMessage = 'Oops an error has occurred: ' . $e->getMessage();  
}catch(EpiOAuthUnauthorizedException $e){  
  // bad authorization..probably bad tokens, do something different  
  $statusMessage = 'Oops an error has occurred: ' . $e->getMessage();  
}catch(EpiOAuthException $e){  
  // uh oh, unknown oauth exception  
  $statusMessage = 'Oops, an unknown authorisation error has occurred! The mojo team have been notified! Please try again.';
}

if(isset($statusMessage)){

}
?>

Any improvement of the above code will also be appreciated.

Thanks all

+2  A: 

Use curl_setopt(CURLOPT_CONNECTTIMEOUT, 1 /* 1 second timeout */); to cause CURL to give up if a connection isn't established in 1 second. I use this when connecting to the facebook API, 'cause they've been pretty unreliable in the past as well.

Frank Farmer
+4  A: 

The library supports a value to be passed into the curl timeout.

$twitterObj->setTimeout($secs_request_timeout);

I just added support for passing in a connection timeout as well. Unable to run unit tests because I'm being rate limited. Will commit this once I can verify that it works.

$twitterObj->setTimeout($secs_request_timeout, $secs_connection_timeout);
Hah, nothing like having the package maintainer add a feature to address your stackoverflow question ;)
Frank Farmer
Thank you very much Jaisen for taking the time to do this. You rock! :)
Abs