PHPでTwitterのタイムラインを取得する
今回、色々な事情を鑑みてこのレンタルサーバーを利用してWebアプリという形態でTwitterクライアントを作成することにしました。Webクライアントの強みはどのプラットフォームでも動作可能など、強みがたくさんあるので、作る価値があるかもしれません。しかも、私の場合は例の「事情」もあるので尚更です。というわけでPHPを利用したTwitterのクライントを作っていくシリーズがこれより始まります。
利用するライブラリ
今回は abraham/twitteroauth というものを使っていきます。このライブラリはTwitterの一通りのAPIが実装されているので、私が目標とするクライアント作成にはもってこいです。
ダウンロードした後、autoload.phpを読み込ませることで利用できるようになります。(本来はcomposer.jsonを使って読み込ませるのが正規ルートです。)
$connection = new TwitterOAuth($api_key, $api_secret, $access_token, $access_token_secret); $statuses = $connection->get("statuses/home_timeline", ["count" => 50]); //タイムラインを50ツイート取得 if(isset($statuses->errors)) { //エラー発生 echo "some error occurred."; echo "error message: " . $statuses->errors[0]->message; } else { //ツイートの取得に成功 for ($i=0; $i < 49; $i++) { echo "profile image url: " . $statuses[$i]->user->profile_image_url; echo "user name: " . $statuses[$i]->user->name; echo "user's id: " . $statuses[$i]->user->screen_name; echo "tweet text: " . $statuses[$i]->text; echo "created at: " . $statuses[$i]->created_at; echo "via: " . $statuses[$i]->source; echo $statuses[$i]->retweet_count . " rt"; echo $statuses[$i]->favorite_count . " fav"; if($statuses[$i]->user->protected) {echo "非公開";} echo "-------------------------------" } }
1行目で取得済みの各キーでTwitterに接続するための前準備を行い、2行目でタイムラインを50ツイート分取得しています。この時、APIのレートリミットを超過したアクションを行うとerrors[]->messageにその内容が帰ってきます。(エラーコードなども取得できるので詳しくはvar_dumpなどを行ってください。)
8行目以降はツイート取得に成功した場合です。最小限で利用するような情報を引き抜いてみました。それぞれのツイートには、ツイートに関する情報が$statuses以下、投稿者に関する情報が$statuses->userにそれぞれ格納されています。
以下は、試しにたまたま流れてきたユーザーの$statuses(Array 配列だけではなく、stdClass オブジェクトでも構成されていることに注意。Arrayは$statuses[“hoge”]と指定するのに対して、オブジェクトは$statuses->hogeと指定。)をvar_dump()で出力してみた結果が以下です。
array(25) { [0]=> object(stdClass)#3 (23) { ["created_at"]=> string(30) "Fri Oct 14 12:44:02 +0000 2016" ["id"]=> int(786910433936420869) ["id_str"]=> string(18) "786910433936420869" ["text"]=> string(30) "コジィさん出るのかな" ["truncated"]=> bool(false) ["entities"]=> object(stdClass)#8 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["user_mentions"]=> array(0) { } ["urls"]=> array(0) { } } ["source"]=> string(81) "Twitter for iPad" ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> object(stdClass)#9 (41) { ["id"]=> int(219588098) ["id_str"]=> string(9) "219588098" ["name"]=> string(9) "ふゆべ ["screen_name"]=> string(11) "who_you_bee" ["location"]=> string(0) "" ["description"]=> string(64) "Tumblr:https://t.co/X4hF7e4Jvi Pixiv:https://t.co/8vCm4L63xm" ["url"]=> string(23) "https://t.co/8vCm4L63xm" ["entities"]=> object(stdClass)#10 (2) { ["url"]=> object(stdClass)#11 (1) { ["urls"]=> array(1) { [0]=> object(stdClass)#12 (4) { ["url"]=> string(23) "https://t.co/8vCm4L63xm" ["expanded_url"]=> string(27) "http://pixiv.me/savoiaxxs21" ["display_url"]=> string(20) "pixiv.me/savoiaxxs21" ["indices"]=> array(2) { [0]=> int(0) [1]=> int(23) } } } } ["description"]=> object(stdClass)#13 (1) { ["urls"]=> array(2) { [0]=> object(stdClass)#14 (4) { ["url"]=> string(23) "https://t.co/X4hF7e4Jvi" ["expanded_url"]=> string(26) "http://huyube21.tumblr.com" ["display_url"]=> string(19) "huyube21.tumblr.com" ["indices"]=> array(2) { [0]=> int(7) [1]=> int(30) } } [1]=> object(stdClass)#15 (4) { ["url"]=> string(23) "https://t.co/8vCm4L63xm" ["expanded_url"]=> string(27) "http://pixiv.me/savoiaxxs21" ["display_url"]=> string(20) "pixiv.me/savoiaxxs21" ["indices"]=> array(2) { [0]=> int(37) [1]=> int(60) } } } } } ["protected"]=> bool(false) ["followers_count"]=> int(8710) ["friends_count"]=> int(668) ["listed_count"]=> int(350) ["created_at"]=> string(30) "Thu Nov 25 08:30:34 +0000 2010" ["favourites_count"]=> int(15994) ["utc_offset"]=> int(32400) ["time_zone"]=> string(5) "Tokyo" ["geo_enabled"]=> bool(false) ["verified"]=> bool(false) ["statuses_count"]=> int(96048) ["lang"]=> string(2) "ja" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["is_translation_enabled"]=> bool(false) ["profile_background_color"]=> string(6) "C0DEED" ["profile_background_image_url"]=> string(79) "http://pbs.twimg.com/profile_background_images/488513744176037888/Jq1mlOeM.jpeg" ["profile_background_image_url_https"]=> string(80) "https://pbs.twimg.com/profile_background_images/488513744176037888/Jq1mlOeM.jpeg" ["profile_background_tile"]=> bool(true) ["profile_image_url"]=> string(74) "http://pbs.twimg.com/profile_images/759076048420032512/nXPEc9QI_normal.jpg" ["profile_image_url_https"]=> string(75) "https://pbs.twimg.com/profile_images/759076048420032512/nXPEc9QI_normal.jpg" ["profile_banner_url"]=> string(58) "https://pbs.twimg.com/profile_banners/219588098/1471816404" ["profile_link_color"]=> string(6) "ABB8C2" ["profile_sidebar_border_color"]=> string(6) "FFFFFF" ["profile_sidebar_fill_color"]=> string(6) "DDEEF6" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["has_extended_profile"]=> bool(false) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> bool(true) ["follow_request_sent"]=> bool(false) ["notifications"]=> bool(false) } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> NULL ["contributors"]=> NULL ["is_quote_status"]=> bool(false) ["retweet_count"]=> int(0) ["favorite_count"]=> int(0) ["favorited"]=> bool(false) ["retweeted"]=> bool(false) ["lang"]=> string(2) "ja" }
想像以上に情報が取得できていて、驚きです。これらの情報を使うことで、クライアントとしての道以外にも使い道が出来ると思うので、ぜひご活用ください。
また、実際にクライアントを作成している様子をこちらのサーバーで公開中なので、ぜひお試しいただければと。
https://yoshipc.net/twitter
投稿者プロフィール
最新の投稿
- 2018.09.01AMD動画エンコードでGPUを活用する
- 2018.03.26phpツイートIDからsnowflakeを用いて投稿時刻のミリ秒を算出
- 2018.02.18phpphpMyAdminのインストール方法(Ubuntu)
- 2018.01.07未分類新聞を折らずに郵送する方法
最近のコメント