Hi, I'm trying to display a number of Twitter followers using PHP given a username. My code looks like this:
function tweet_count() {
$name = get_option('ws_twit');
$twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml');
$begin = '<followers_count>'; $end = '</followers_count>';
$page = $twit;
$parts = explode($begin,$page);
$page = $parts[1];
$parts = explode($end,$page);
$tcount = $parts[0];
if($tcount == '') { $tcount = '0'; }
echo $tcount;
}
It usually works... except when it doesn't. Most of the time, it throws out an error:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in [filename] on line 8
Line 8 is: $twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml');
I tried hardcoding the username, but the effect is the same. I'm wondering if this code is okay, since it DOES sometimes work. First I assumed that maybe it was Twitter error but it happens way to often to be the case.
Thanks for your help!