2

I am using the Twitter API to display the statuses of a user. However, in some cases (like today), Twitter goes down and takes all the APIs with it. Because of this, my application fails and continuously displays the loading screen.

I was wondering if there is a quick way (using PHP or JS) to query Twitter and see if it (and the API) is up. I'm thinking it could be an easy response of some sort.

Thanks in advance, Phil

3 Answers 3

6

Request http://api.twitter.com/1/help/test.xml or test.json. Check to make sure you get a 200 http response code.

If you requested XML the response should be:

<ok>true</ok>

The JSON response should be:

"ok"
0
1

JSONP!

You can have some function like this, declared in the head or before including the next script tag below:

var isTwitterWorking = false;

function testTwitter(status) {
    if (status === "ok") {
        isTwitterWorking = true;
    }
}

And then

<script src="http://api.twitter.com/1/help/test.json?callback=testTwitter"></script>

Demo (might take a while, Twitter's API seems to be slow here)

0
function visit($url) {

    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();

    curl_setopt ($ch, CURLOPT_URL,$url );
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch,CURLOPT_VERBOSE,false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    $page=curl_exec($ch);
    //echo curl_error($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if($httpcode>=200 && $httpcode<300)
        return true;
    else 
        return false;
}

// Examples
if(visit("http://www.twitter.com"))
   echo "Website OK"."n"; // site is online
else
   echo "Website DOWN"; // site is offline / show no response

I hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.