codebird.php の利用
#05 Codebirdを使ってみよう
http://dotinstall.com/lessons/more_php_v2/21805
を参考に
twitter API へ codebird.php を使うことでアクセスする
今回編集するのは more.php
まず、必要なファイルの読み込み
1 2 | require_once ( 'codebird.php' ); require_once ( 'config.php' ); |
codebird.php については
https://github.com/jublonet/codebird-php
を参照
require_once() でライブラリを読み込んで
1 2 | \Codebird\Codebird::setConsumerKey( 'YOURKEY' , 'YOURSECRET' ); $cb = \Codebird\Codebird::getInstance(); |
で初期化
今回は、アクセストークンもあるので
1 | $cb ->setToken( 'YOURTOKEN' , 'YOURTOKENSECRET' ); |
も追加する
ただし、設定ファイルである config.php に定数で設定しているため
‘YOURKEY’
は
COMSUMER_KEY
‘YOURSECRET’
は
COMSUMER_SECRET
へ変更する
また
アクセストークンのところも
‘YOURTOKEN’
を
ACCESS_TOKEN
‘YOURTOKENSECRET’
を
ACCESS_TOKEN_SECRET
とする
これで、codebird が使えるようになる
使い方については
https://github.com/jublonet/codebird-php
の
3. Mapping API methods to Codebird function calls
のところを参照
一部引用すると
For each slash in a Twitter API method, use AngularJS underscore in the Codebird function.
Example: statuses/update maps to Codebird::statuses_update().
For each underscore in a Twitter API method, use camelCase in the Codebird function.
Example: statuses/home_timeline maps to Codebird::statuses_homeTimeline().
というように関数名のときには
/ を _ に置き換えて
_ は 次の文字を大文字にする
この英文の例なら
tatuses/home_timeline
の場合
1 | statuses_homeTimeline() |
となっている
今回、使う twitter API は
ユーザのつぶやきを取得するので
https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
に載っている
user_timeline api を使う
$cb は codebird の意味
1 | $tweets = $cb ->statuses_userTimeline(); |
とすれば
$tweets へつぶやきを格納できる