pear を使ってTwitterのOAuth 認証をさせるアプリを作ることになりました。PEARというのは、PHP Extension and Application Repository のことで、要するにPHP のライブラリ群ですね。http://pear.php.net/ が公式サイトです。かなり便利!
さて、これをどうやって使うのかということでインストール方法をまとめてみました。
環境整備
Mac OS X Lion の場合
※※このあたり全部書き直しました。(2012/02/16 11:00 くらい更新)
色々なサイトに書いてある方法が通用しなかった。具体的にはyour php version is too new!! といったエラーが出てgo-pear が使えない。そこで、別の方法を模索するとこんな感じでインストールできた。(http://sudhanshuraheja.com/2011/03/installing-php-pear-on-mac-osx-10-6-with-php-5-3-3/ を参考にした。英語です)
$ curl http://pear.php.net/go-pear.phar > go-pear.phar $ sudo php -d detect_unicode=0 go-pear.phar // これで以下の設定画面に入ることができる。 Below is a suggested file layout for your new PEAR installation. To change individual locations, type the number in front of the directory. Type 'all' to change all of them or simply press Enter to accept these locations. 1. Installation base ($prefix) : /usr 2. Temporary directory for processing : /tmp/pear/install 3. Temporary directory for downloads : /tmp/pear/install 4. Binaries directory : /usr/bin 5. PHP code directory ($php_dir) : /usr/share/pear 6. Documentation directory : /usr/docs 7. Data directory : /usr/data 8. User-modifiable configuration files directory : /usr/cfg 9. Public Web Files directory : /usr/www 10. Tests directory : /usr/tests 11. Name of configuration file : /private/etc/pear.conf // 以下のように設定しました。 1. Installation base ($prefix) : /usr/share/pear 2. Temporary directory for processing : /tmp/pear/install 3. Temporary directory for downloads : /tmp/pear/install 4. Binaries directory : /usr/bin 5. PHP code directory ($php_dir) : /usr/share/pear 6. Documentation directory : /usr/share/pear/docs 7. Data directory : /usr/share/pear/data 8. User-modifiable configuration files directory : /usr/share/pear/cfg 9. Public Web Files directory : /usr/share/pear/www 10. Tests directory : /usr/share/pear/tests 11. Name of configuration file : /private/etc/pear.conf
最後に、php.iniの編集です。include_path に先ほど設定した$php_dir を設定します。(今回の場合は/usr/share/pear)
これでPEARコマンドが動くはず。ということで、早速Services_Twitterを入れてみます。
# pear install --force --alldeps Services_Twitter // 中略 install ok: channel://pear.php.net/Net_URL2-2.0.0 install ok: channel://pear.php.net/Log-1.12.7 install ok: channel://pear.php.net/Cache_Lite-1.7.12 install ok: channel://pear.php.net/DB-1.7.14 install ok: channel://pear.php.net/MDB2-2.4.1 install ok: channel://pear.php.net/Mail-1.2.0 install ok: channel://pear.php.net/Net_Socket-1.0.10 install ok: channel://pear.php.net/Auth_SASL-1.0.6 install ok: channel://pear.php.net/HTTP_Request2-2.0.0 install ok: channel://pear.php.net/Net_SMTP-1.6.1 install ok: channel://pear.php.net/Services_Twitter-0.6.3 install ok: channel://pear.php.net/HTTP_OAuth-0.2.3
入った!!やった!!
さて、実際にコードを書いたら動くのか??ということで以下のようなコードを描いてみた。といっても、@IT に書いてあったコード丸パクリですが。
// Test of Twitter require_once 'Services/Twitter.php'; $twitter = new Services_Twitter(); $twits = $twitter->statuses->public_timeline(); ?> <html> <body> <table> <tr> <td>日付</td> <td>画像</td> <td>ユーザー</td> <td>つぶやき</td> </tr> <?php foreach($twits as $twit): ?> <tr> <td><?=$twit->created_at?></td> <td><img src="<?=$twit->user->profile_image_url?>" width=48 height=48></td> <td><a href="<?=$twit->user->url?>"><?=$twit->user->name?></a></td> <td><?=$twit->text?></td> </tr> <?php endforeach;?> </table> </body> </html>
Ubuntu の場合
$ sudo su // ルートユーザーになる # apt-get install php-pear # pear update pear // 更新の確認 # pear install --force --alldeps Services_Twitter
コマンドラインベースでこうやって簡単に入れれます。Linux触り始めた頃は、Linux難しいコマンドライン訳が分からないよって嘆いていましたが、最近コマンドラインがいかに便利か分かって来ました。爆
もしこれで正常に動かないようでしたら、phpの設定ファイルを編集する必要があります。include_path にpearがインストールされているパスを指定してやると動きます。