エントリー

Twitterでフォローされている人を一覧表示

Twitter API 仕様書
http://watcher.moe-nifty.com/memo/docs/twitterAPI.txt

statuses/followers の項目にフォロワーを取得する方法は書かれていますが、APIの仕様上100件までしか取得できません。ですがAPIから送られてくる next_cursor の値を使えば、続きを取得することができます。

そんな訳で書いてみたのが以下のプログラム。フォロワーをすべて取得しますが、APIを何度も呼び出すのでフォロワーが多いと重くなります。

<?php

function get_followers($screen_name)
{
  $results = array();
  $cursor  = -1;

  while (1) {
    $xml = simplexml_load_string(file_get_contents('http://api.twitter.com/1/statuses/followers/' . $screen_name . '.xml?cursor=' . $cursor));

    foreach ($xml->users->user as $user) {
      $results[] = (string)$user->screen_name;
    }

    $cursor = $xml->next_cursor;

    if ($cursor == 0) {
      break;
    }
  }

  return $results;
}

?>

以下のようにID(正確にはスクリーンネーム)を指定すると、フォロワー一覧を返してくれます。なお、鍵をかけているユーザーの情報は取得できません。

<?php

$followers = get_followers('favoritelabo');

print_r($followers);
exit;

?>

ページ移動

ユーティリティ

カテゴリー

検索

エントリー検索フォーム
キーワード

過去ログ

過去ログ表示フォーム
キーワード

Feed