[英]Twitter API: Get Followers +99
使用twitter API(和OAuth)如果我要呼叫用戶關注者,(狀態/關注者)我將只返回99個結果。
有沒有辦法可以返回99,然后從追隨者100再次打電話然后循環通過這種呼叫方式,直到追隨者的總數被返回?
或者只返回所有粉絲?
您需要指定游標參數,如API文檔中所述 。 例如,指定cursor = -1來請求第一頁,然后使用第一個響應中返回的next_cursor值:
http://twitter.com/statuses/followers/barackobama.xml?cursor=-1
http://twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903
<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/fawadghafoor.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$name = $friends['name'];
?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php } ?>
Twitter API限制我們使api調用方法關注者/ ids是每15分鍾15個請求。 如果你做的超過這個api將給你一個錯誤消息,達到速率限制。
有關twitter API速率限制的更多信息,請訪問https://dev.twitter.com/docs/rate-limiting/1.1和https://dev.twitter.com/docs/rate-limiting/1.1/limits
確保您使用的是正確的電話。 followers/ids
給你5000(但它只是一個id列表)。 此調用也使用光標讓您逐步瀏覽用戶頁面。 當你擁有它們時,你會得到零回報。
Twitter每小時只允許一定數量的API請求,我覺得分鍾。 您可能無法一次檢索超過99個請求。
雖然我不久前問過這個問題,但最近我又回來建立了一些非常類似的東西(+新的編程技巧)。
我注意到Twitter API有一種方法可以在一個請求中獲取所有用戶的關注者(或關注者)用戶ID。 我找到了最好的辦法是array_chunk
標識的成批次100(只有我不想使用所有小時的用戶API請求采取的第一個30個陣列-他們可能想實際鳴叫)。 然后有一個方法,允許您獲得最多100個用戶userinfo(從當前經過身份驗證的用戶的視圖)所以我只是做一個循環(在中間睡一會兒),然后你有30,000個Twitter粉絲!
我建議在隊列系統中異步執行此操作,就好當您在用戶請求站點上的頁面時動態執行此操作時,它可能非常慢並且您可能容易發生HTTP超時。 也像地獄一樣緩存它們!
對不起,我沒有發布任何代碼,但希望這個思考過程會幫助別人:)
$cursor = -1;
$account_from = 'twitter_account';
do
{
$json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'json?cursor=' . $cursor);
$accounts = json_decode($json);
foreach ($accounts->users as $account)
{
array(
':twitter_id' => $account->id_str,
':account' => $account->screen_name,
':description' => $account->description,
);
}
$cursor = $accounts->next_cursor;
}
while ($cursor > 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.