TwitterのOAuth認証

twitteroauthの設定とタイムラインを取得する簡易クラス

twitteroauthのダウンロード

abraham / twitteroauth から最新のZipファイル(twitteroauth-master.zip)をダウンロード。

Twitterアプリケーションの申請

Twitter Developersから Sign in を押し、Sign in with your Twitter accountへ移動。

Sign in with your Twitter account
Twitterのアカウントでログイン。
ログイン後、Twitter DevelopersからMy applicationsへ移動。

My applications
create new application を押し、Create an applicationへ移動。

Create an application
Application Details
Name
アプリケーションの名称(ツイートしたときに表示される使用しているアプリの名前)を設定
Description
アプリケーションの詳細を設定
Website
アプリケーションのURL(ツイートしたときに表示される使用しているアプリのリンク先)を設定
Developer Rules of the Road
Yes, I agree をチェック
CAPTCHA
表示されている文字列を入力。
create your twitter application を押し、アプリケーションを作成。

OAuth settings
新規生成されたアプリケーションの情報が表示されるので、Consumer keyとConsumer secretを取得。

Your access token
create my access token を押し、Access tokenとAccess token secretを取得。

twitteroauth.phpの設定

API 1.0からAPI 1.1へ変更

/* Set up the API root URL. */ public $host = "https://api.twitter.com/1.1/"; // public $host = "https://api.twitter.com/1/";

タイムラインの取得

Consumer key、Consumer secret、Access token、Access token secretを設定。
http://php.o0o0.jp/twitterをたたくと処理結果が返ってくるアクションコントローラのクラスを作成。

PHP

require_once('/twitteroauth/twitteroauth.php'); /** * Twitter oAuth */ class twitterController { public $consumer_key = 'xxxxxxxxxxxxxxxxxxxxxx'; public $consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; public $access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; public $access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; /** * コンストラクタ */ public function __construct() { $this->twitteroauth = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $this->access_token, $this->access_token_secret); } /** * 処理 */ public function indexAction() { $response = $this->twitteroauth->get('statuses/home_timeline', array('count' => 10)); print_r($response); exit; } }

$response
このままprint_rするとおそらく文字化けが発生するので、header('Content-Type: text/html; charset=UTF-8');を設定しておくと解消。

Array ( [0] => stdClass Object ( [created_at] => Mon Nov 19 00:00:00 +0000 2012 [id] => 000000000000000000 [id_str] => 000000000000000000 [text] => ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○ [source] => SOICHA [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] => [user] => stdClass Object ( [id] => 00000000 [id_str] => 00000000 [name] => ○○○○○○○○ [screen_name] => xxxxxxxxxxxxxxxx [location] => [description] => ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○ [url] => [entities] => stdClass Object ( [description] => stdClass Object ( [urls] => Array ( ) ) ) [protected] => [followers_count] => 000 [friends_count] => 000 [listed_count] => 0 [created_at] => Mon Sep 07 00:00:00 +0000 2009 [favourites_count] => 1 [utc_offset] => 32400 [time_zone] => Tokyo [geo_enabled] => [verified] => [statuses_count] => 9 [lang] => ja [contributors_enabled] => [is_translator] => [profile_background_color] => C0DEED [profile_background_image_url] => http://a0.twimg.com/profile_background_images/000000000/flow1.jpg [profile_background_image_url_https] => https://si0.twimg.com/profile_background_images/000000000/flow1.jpg [profile_background_tile] => [profile_image_url] => http://a0.twimg.com/profile_images/0000000000/xxxxxxxxxxxxxxxx_normal.jpg [profile_image_url_https] => https://si0.twimg.com/profile_images/0000000000/xxxxxxxxxxxxxxxx_normal.jpg [profile_banner_url] => https://si0.twimg.com/profile_banners/00000000/0000000000 [profile_link_color] => 0084B4 [profile_sidebar_border_color] => C0DEED [profile_sidebar_fill_color] => DDEEF6 [profile_text_color] => 333333 [profile_use_background_image] => 1 [default_profile] => 1 [default_profile_image] => 1 [following] => 1 [follow_request_sent] => [notifications] => ) [geo] => [coordinates] => [place] => [contributors] => [retweet_count] => 0 [entities] => stdClass Object ( [hashtags] => Array ( ) [urls] => Array ( ) [user_mentions] => Array ( ) ) [favorited] => [retweeted] => ) (略) )

APIが1.0から1.1に切り替わった当時に試したものであり、現在も継続して正常に動作するかは不明です。

最新の記事

プロフィール

流されるままにウェブ業界で仕事しています。主にLAPP環境でPHPを書いています。最近はjQueryで遊んでいます。
※動作確認について