Draft of List API documentation Options @ Twitter API Announcements
これ、草案らしいですけど一通りTwitterのリスト機能を操作するための仕様が書かれていたので、試しに使ってみました。
ここで書いたAPIは正しく機能したんですが、なんせ草案とのことなので今後仕様が変わるかもしれませんね・・・。
ベースにしているのは、以前に書いた下記の2つの記事のプログラムで、PEARのHTTP/Client.phpを使っています。
自分が作成したリストを取得
まずは、自分が作成しているリストを取得してみます。
リストは、publicなものもprivateなものも両方取得できます。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // 自分のリスト一覧を取得 $page = "it_boy/lists.xml"; // 認証 $basic = array('Authorization'=>'Basic '.base64_encode($username.':'.$password)); $client = new HTTP_Client(null, $basic); $client->get("http://twitter.com/".$page ); // レスポンスを取得 $response = $client->currentResponse(); $body = mb_convert_encoding($response['body'], "EUC-JP","auto"); $xml = new SimpleXMLElement($body); // リスト名とそのフルネーム、リストのモード(public or private)と登録メンバー数を表示 for ($i = 0; $i < count($xml->lists->list); $i++) { echo "name / full_name: " .$xml->lists->list[$i]->name . " / " . $xml->lists->list[$i]->full_name . "\n"; echo "mode / member: " . $xml->lists->list[$i]->mode . " / " . $xml->lists->list[$i]->member_count . "\n"; } ?>
実行結果は、こんな感じに現在作成している2つのリストが取得できます。
$ php twitter_mylist.php name / full_name: favorite_user / @it_boy/favorite-user mode / member: private / 1 name / full_name: engineer / @it_boy/engineer mode / member: public / 6
自分が登録されているリストを取得
次は、結構気になる(?)自分が登録されているリスト一覧を取得するスクリプトです。
実行結果は、さっきと同様に出るので割愛。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // 自分が追加されているリスト一覧 $page = "it_boy/lists/memberships.xml"; $client = new HTTP_Client(null, $basic); $client->get("http://twitter.com/".$page ); // レスポンスを取得 $response = $client->currentResponse(); $body = mb_convert_encoding($response['body'], "EUC-JP","auto"); $xml = new SimpleXMLElement($body); // リスト名とそのフルネーム、リストの登録メンバー数を表示 for ($i = 0; $i < count($xml->lists->list); $i++) { echo "name / full_name: " .$xml->lists->list[$i]->name . " / " . $xml->lists->list[$i]->full_name . "\n"; echo "member: " . $xml->lists->list[$i]->member_count . "\n"; } ?>
該当のリストに登録されているメンバーのつぶやきを取得する
お次はリストのメイン機能になる、リストに登録した人のつぶやきを取得するスクリプトです。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // そのリストに登録している人のつぶやきを見る $page = "it_boy/lists/engineer/statuses.xml"; // 認証 $basic = array('Authorization'=>'Basic '.base64_encode($username.':'.$password)); $client = new HTTP_Client(null, $basic); $client->get("http://twitter.com/".$page ); // レスポンスを取得 $response = $client->currentResponse(); $body = mb_convert_encoding($response['body'], "EUC-JP","auto"); $xml = new SimpleXMLElement($body); // 「アカウント名 : 日付」の下に発言を表示する for ($i = 0; $i < count($xml->status); $i++) { echo $xml->status[$i]->user->screen_name . " : " . date('Y/m/d H:i:s', strtotime($xml->status[$i]->created_at)) . "\n"; echo "\t" . $xml->status[$i]->text . "\n"; } ?>
実行結果は割愛しますが、「Twitterのタイムラインを取得するPHPスクリプト
」と全く同じイメージで出力されます。
リストを作成する
お次は、新しくリストを作ってみます。
今までとはちょっと作りが違ってきます。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // TwitterAPI用のURL $url = "http://twitter.com/it_boy/lists.xml"; // リスト名とモード(public or private)を配列にセット $data = array("name" => "hoge_list", "mode" => "public"); $basic = array('Authorization'=>'Basic '.base64_encode($username.':'.$password)); $client = new HTTP_Client(null, $basic); $client->post($url, $data); $response = $client->currentResponse(); ?>
これで、実行してみたら、上記の場合は「hoge_list」がpublicモードで作成されます。
リストにメンバーを加える
リストの作成が終ったら、メンバーを登録しなくては意味がありません。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // TwitterAPI用のURL $url = "http://twitter.com/it_boy/engineer/members.xml"; // 登録するユーザーのIDをセット $data = array("id" => "12345678"); $basic = array('Authorization'=>'Basic '.base64_encode($username.':'.$password)); $client = new HTTP_Client(null, $basic); $client->post($url, $data); $response = $client->currentResponse(); ?>
登録するに当たっては、ユーザー名ではなくユーザーIDを指定します。
なので、別の手段でユーザーIDを事前に取得しておかなくてはなりません。
その人の発言内容を取得したら、その中にユーザーIDは必ず含まれているのでそこから取得してセットすればよいかもしれません。
リストからメンバーを削除する
最後は、リストに追加したメンバーを残念ながら削除する場合です。
<?php require_once "HTTP/Client.php"; $username = "it_boy"; $password = "hogehoge"; // パラメータに削除するユーザーIDを付与しておく $url = "http://twitter.com/it_boy/engineer/members.xml?id=12345678"; $basic = array('Authorization'=>'Basic '.base64_encode($username.':'.$password)); $client = new HTTP_Client(null, $basic); $client->delete($url); $response = $client->currentResponse(); ?>
削除の場合も、ユーザーIDを指定する必要があります。
また、削除の場合だけDELTEメソッドを発行して削除を行います。
主機能の使い方は書いてみましたが、最初に書いたAPIの草案の中には、ここに書いた以外のAPIも書かれているので試してみてはどうでしょうか。
最後に、よろしければ@it_boy をよろしくお願いします。
関連記事
[PHP] デーモンとして動かせるTwitter botの作り方
Twitterのフレンドやフォロワーを取得するPHPスクリプト
Twitterのお気に入りをTumblrへ投稿するプログラム書いたよ