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

2015年5月14日

サービス プログラミング 特集

前回、簡単な投稿BOTを作りましたが、twitter-botの真骨頂は、
  1. ツイート処理
  2. ユーザーフォロー
  3. フォロワーの獲得
  4. フォロワーにメッセージ送信
こういった事をほぼ全て自動化することに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サーバーに対する操作

自分の情報を取得

<?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);

実行

とりあえず、ターミナルで確認します。 php twitter_userinfo.php uid=*user-name* # *user-name*は自分のtwitterアカウントを入れてみてください。今回の結果は「geta1972」を入れてみました。

結果

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] => ) なかなかしっかりした情報が得られます。 次回はこれを基本として、情報収集していきます。

人気の投稿

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。

ブログ アーカイブ