2012-01-31 50 views
0

我可以很容易地使用PHP和JSON獲取用戶推文,但只要我用它來獲取關注者列表,就會收到錯誤信息。兩者都使用JSON來返回值。在PHP中使用JSON獲取Twitter追隨者?

的代碼是:

$jsonurl = "https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=mooinooicat"; 
$contents = file_get_contents($jsonurl); 
$results = json_decode($contents, true); 

echo "<pre>"; 
print_r($results); 
echo "</pre>"; 

這給了我下面的數組:

Array 
(
    [next_cursor] => 0 
    [ids] => Array 
     (
      [0] => 31085924 
      [1] => 53633023 
      [2] => 18263583 
     ) 

    [previous_cursor] => 0 
    [next_cursor_str] => 0 
    [previous_cursor_str] => 0 
) 

如何獲得next_cursor和previous_cursor的價值觀和我怎麼循環剛剛經歷的ID陣列?

我想解析讀取到數據庫中的結果。

+0

看看http://php.net/manual/en/language.types.array.php – 2012-01-31 11:36:46

+0

@andrebruton https://api.twitter.com/1/followers/ids.json它向我們提供了我提供的屏幕名稱追隨者的ID,我們可以替換屏幕名稱嗎? – 2012-07-30 23:57:22

+0

請參閱:[Twitter追隨者API教程](https://dev.twitter.com/rest/reference/get/followers/ids) – kenorb 2015-05-30 22:45:41

回答

0

感謝沒有例子的所有問題的答案...

我終於成功地從Getting values from a single array

這裏弄明白使用示例代碼如下:

foreach ($results as $result) { 
    if (is_array($result)) { 
    foreach ($result as $sub_result) { 
     // You can store this value in a variable, or output it in your desired format. 
     echo $sub_result . "<br />"; 
    } 
    } else { 
    echo $result . "<br />"; 
    } 
} 
+0

如果您想要數組鍵和值,請使用:foreach($ result as $ key => $ value){然後鍵名是$ key,值是$ value – andrebruton 2012-02-01 09:05:59

1

不使用正確的API嘗試這樣的事情

function fetch_twitter_count($user) { 
    if ($json = file_get_contents("http://api.twitter.com/1/users/show.json?screen_name=$user")) { 
     if(empty($json)) return 0; 

     $json = json_decode($json['body'], true); 

     return number_format(intval($json['followers_count'])); 
    } 
    return 'API Error'; 
} 

沒有t測試,但應該做你想做的事情但是保持聯繫,你會想要使用某種緩存

相關問題