Twitterのbotを作る #3「twitterOAuthの基本操作」twitter,

前回、簡単な投稿BOTを作りましたが、twitter-botの真骨頂は、
- ツイート処理
- ユーザーフォロー
- フォロワーの獲得
- フォロワーにメッセージ送信
こういった事をほぼ全て自動化することにbotを設置する意味があるということで、
最終は、上記を全て自動化して、ブロガーの人は、「ブログPVの増加」、アフェリエイトやECの方は「顧客の獲得(売上増加)」を目指す為の、botを作りたいと思います。
今回は上記を実現させるための下準備という事で、前回作ったbot環境を元に、ライブラリを通して、twitterサーバーに対して行える操作をまとめたいと思います。
環境設定
サーバーはdebianでもubuntuでもcentosでもLINUXであれば何でもOKでしょう。
サーバー環境
- Apache
- PHP
- TwitterOAuthライブラリ(https://github.com/abraham/twitteroauthからDL)
- twitterアカウント情報「consumer_key」「consumer_secret」「access_token」「access_token_secret」
ファイル構成
- twitteroauth/(DLしたライブラリを設置)
- twitter_userinfo.php
※ここまでは前回設置した情報と同じです。
twitterサーバーに対する操作
自分の情報を取得
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php date_default_timezone_set('Asia/Tokyo'); //ライブラリ処理 require 'twitteroauth/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; //CLIの場合argvをREQUESTに変換する。 if(!isset($_SERVER['SCRIPT_URI']) && isset($argv)){ for($i=0,$c=count($argv);$i<$c;$i++){ if(!$argv[$i]){continue;} //各クエリの分解 $q = explode("=",$argv[$i]); if(count($q)<2){continue;} if($q[0]!=''){ //requestに格納 $key = $q[0]; $val = join("=",array_slice($q,1)); $_REQUEST[$key]=$val; } } } //twitterアカウントで置き換えてください。 $key = array( "consumer_key"=>"******", "consumer_secret"=>"******", "access_token"=>"******-******", "access_token_secret"=>"******" ); $connection = new TwitterOAuth($key['consumer_key'],$key['consumer_secret'],$key['access_token'],$key['access_token_secret']); $userinfo = $connection->get('users/show', ['screen_name'=> $_REQUEST['uid']]); print_r($userinfo); |
実行
とりあえず、ターミナルで確認します。
1 2 |
php twitter_userinfo.php uid=*user-name* # *user-name*は自分のtwitterアカウントを入れてみてください。今回の結果は「geta1972」を入れてみました。 |
結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
stdClass Object ( [id] => 11363242 [id_str] => 11363242 [name] => 下駄 [screen_name] => geta1972 [location] => 東京都 [profile_location] => [description] => 妄想発明家 IT関連の開発やアイデアをどんどん創って行く事が使命です。 javascript / php / ajax / RaspberryPi /DIY / API-Service / IoT / nodeJs / 雑学 / Linux / iPhone / Mac / WebMarketing / [url] => http://t.co/zq34sAeSHJ [entities] => stdClass Object ( [url] => stdClass Object ( [urls] => Array ( [0] => stdClass Object ( [url] => http://t.co/zq34sAeSHJ [expanded_url] => http://wordpress.ideacompo.com/ [display_url] => wordpress.ideacompo.com [indices] => Array ( [0] => 0 [1] => 22 ) ) ) ) [description] => stdClass Object ( [urls] => Array ( ) ) ) [protected] => [followers_count] => 5798 [friends_count] => 6370 [listed_count] => 75 [created_at] => Thu Dec 20 06:04:41 +0000 2007 [favourites_count] => 1061 [utc_offset] => 32400 [time_zone] => Tokyo [geo_enabled] => 1 [verified] => [statuses_count] => 4472 [lang] => ja [status] => stdClass Object ( [created_at] => Sat May 09 11:30:04 +0000 2015 [id] => 597000577545965569 [id_str] => 597000577545965569 [text] => ブログ記事紹介:自分にどれだけ自信が持てますか? http://t.co/tbJ6c5BiGZ http://t.co/5nuqlrgRn5 [source] => <a href="http://ideacompo.com/tools/bot/" rel="nofollow">ideacompo.com</a> [truncated] => [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [geo] => [coordinates] => [place] => [contributors] => [retweet_count] => 0 [favorite_count] => 0 [entities] => stdClass Object ( [hashtags] => Array ( ) [symbols] => Array ( ) [user_mentions] => Array ( ) [urls] => Array ( [0] => stdClass Object ( [url] => http://t.co/tbJ6c5BiGZ [expanded_url] => http://bit.ly/1GYKyLR [display_url] => bit.ly/1GYKyLR [indices] => Array ( [0] => 25 [1] => 47 ) ) ) [media] => Array ( [0] => stdClass Object ( [id] => 597000575201378305 [id_str] => 597000575201378305 [indices] => Array ( [0] => 48 [1] => 70 ) [media_url] => http://pbs.twimg.com/media/CEj43C-VAAEC8Yv.jpg [media_url_https] => https://pbs.twimg.com/media/CEj43C-VAAEC8Yv.jpg [url] => http://t.co/5nuqlrgRn5 [display_url] => pic.twitter.com/5nuqlrgRn5 [expanded_url] => http://twitter.com/geta1972/status/597000577545965569/photo/1 [type] => photo [sizes] => stdClass Object ( [medium] => stdClass Object ( [w] => 600 [h] => 305 [resize] => fit ) [small] => stdClass Object ( [w] => 340 [h] => 173 [resize] => fit ) [large] => stdClass Object ( [w] => 1024 [h] => 521 [resize] => fit ) [thumb] => stdClass Object ( [w] => 150 [h] => 150 [resize] => crop ) ) ) ) ) [favorited] => [retweeted] => [possibly_sensitive] => [lang] => ja ) [contributors_enabled] => [is_translator] => [is_translation_enabled] => [profile_background_color] => 131516 [profile_background_image_url] => http://abs.twimg.com/images/themes/theme14/bg.gif [profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme14/bg.gif [profile_background_tile] => 1 [profile_image_url] => http://pbs.twimg.com/profile_images/652250227/ds_zelda_daichi_no_kiteki_0_normal.jpg [profile_image_url_https] => https://pbs.twimg.com/profile_images/652250227/ds_zelda_daichi_no_kiteki_0_normal.jpg [profile_link_color] => 009999 [profile_sidebar_border_color] => EEEEEE [profile_sidebar_fill_color] => EFEFEF [profile_text_color] => 333333 [profile_use_background_image] => 1 [default_profile] => [default_profile_image] => [following] => [follow_request_sent] => [notifications] => [suspended] => [needs_phone_verification] => ) |
なかなかしっかりした情報が得られます。
次回はこれを基本として、情報収集していきます。