我試圖顯示一些使用PHP給定用戶名的Twitter追隨者。我的代碼看起來像這樣:Bulletproof Twitter追隨者與PHP計數?
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;
}
它通常工作...除非它不。在大多數情況下,它拋出一個錯誤:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in [filename] on line 8
8號線是:$twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml');
我試着硬編碼的用戶名,但效果是一樣的。我想知道這段代碼是否正常,因爲它有時可以工作。首先,我認爲這可能是Twitter的錯誤,但它經常發生這種情況。
感謝您的幫助!
你怎麼樣使用XML解析器?代碼將更加可讀。 – 2010-07-01 15:25:59
有趣的是,我在另一個站點上使用* exact *相同的代碼,而另一個域則一直運行。任何人都知道爲什麼? – Justine 2010-07-01 16:22:13