开发者

Check if Twitter API is down (or the whole site is down in general)

开发者 https://www.devze.com 2023-03-06 00:14 出处:网络
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 continuou

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 so开发者_如何学Pythonrt.

Thanks in advance, Phil


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"


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消