Mac OS XにPEARのインストール
Mac OS XにPEARのインストール – ぱんぴーまっしぐら
うまく行かなくなってたので。
$ curl -O http://pear.php.net/go-pear.phar $ php go-pear.phar
パイプでつなぐとreadlineがうまくいかないので別で実行。
ユーザー環境にインストールしようとするのでそのままEnter
~/pear/bin/pear help
Mac OS XにPEARのインストール – ぱんぴーまっしぐら
うまく行かなくなってたので。
$ curl -O http://pear.php.net/go-pear.phar $ php go-pear.phar
パイプでつなぐとreadlineがうまくいかないので別で実行。
ユーザー環境にインストールしようとするのでそのままEnter
~/pear/bin/pear help
twitter利用時に画像ストレージとして利用されることが多いTwitPic。
以前は、twitterのID、パスワードを投げる仕様でしたが、OAuth対応後は、token、keyを投げる仕様となっていました。
token、keyをまんま投げたら認証キーを削除できる程度で、ID、パスワード投げるのと変わらないんですけどね。
TwitPic Developers – API Documentation – API v2 » upload
v2ではtwitterとOAuth認証で利用するヘッダを「X-Verify-Credentials-Authorization」として、リクエストURIを「X-Auth-Service-Provider」としてヘッダに含めることでAPIを利用できるようになりました。
以下、PEAR::HTTP_Request2とPEAR::HTTP_OAuthを利用したサンプル。
<?php
require_once 'HTTP/OAuth/Consumer.php';
define('OAUTH_CONSUMER_KEY', 'xxxx');
define('OAUTH_CONSUMER_SECRET', 'xxxx');
define('OAUTH_TOKEN', 'xxxx');
define('OAUTH_TOKEN_SECRET', 'xxxx');
define('TWITPIC_KEY', 'xxxx');
try {
$consumer_request = new HTTP_OAuth_Consumer_Request();
$consumer_request->accept(new HTTP_Request2());
$consumer = new HTTP_OAuth_Consumer(OAUTH_CONSUMER_KEY,
OAUTH_CONSUMER_SECRET,
OAUTH_TOKEN,
OAUTH_TOKEN_SECRET);
$consumer->accept($consumer_request);
$resp = $consumer->sendRequest('https://api.twitter.com/1/account/verify_credentials.json', array(), HTTP_Request2::METHOD_GET);
$headers = $consumer->getLastRequest()->getHeaders();
$http_request = new HTTP_Request2();
$http_request->setHeader('X-Auth-Service-Provider', 'https://api.twitter.com/1/account/verify_credentials.json');
$http_request->setHeader('X-Verify-Credentials-Authorization', $headers['authorization']);
$http_request->setUrl('http://api.twitpic.com/2/upload.json');
$http_request->setMethod(HTTP_Request2::METHOD_POST);
$http_request->addPostParameter('key', TWITPIC_KEY);
$http_request->addPostParameter('message', 'test');
$http_request->addUpload('media', '/path/to/image');
$resp = $http_request->send();
$body = $resp->getBody();
$body = json_decode($body);
var_dump($body);
} catch (Exception $e) {
var_dump($e->getMessage());
}
twitterとのOAuth認証に必要なAuthorizationヘッダを生成するだけでよいので、実際はtwitter側にリクエストする必要はないのですが、ソース読んだところいろいろ面倒なのでとりあえずこんな形で。
PHP5.3.3では以下のような「後方非互換の変更」が加えられています。
Methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn’t affect non-namespaced classes.
<?php namespace Foo; class Bar { public function Bar() { // treated as constructor in PHP 5.3.0-5.3.2 // treated as regular method in PHP 5.3.3 } } ?>There is no impact on migration from 5.2.x because namespaces were only introduced in PHP 5.3.
PHP 5.3.3 Released!
どういう事かというと、
<?php
class A
{
function A()
{
echo __CLASS__;
}
}
class B extends A
{
}
class C extends B
{
function C()
{
parent::B();
}
}
new C();
結果
PHP5.3.2
PHP Fatal error: Call to undefined method B::B()
PHP5.3.3
A
PHP5.3.xによってPHP4に対する後方互換が崩れたんですが、PHP5.3.3でPHP5.3.x非互換となる代わりにPHP4後方互換が一部戻ったという話。
PEAR::Image_Graphで問題でてました。
PHP5の環境なら以下のように書くので問題は起こらないんですけどね。
<?php
class A
{
public function __construct()
{
echo __CLASS__;
}
}
class B extends A
{
}
class C extends B
{
public function __construct()
{
parent::__construct();
}
}
new C();
Services_Twitter_r302262.diff
7904 byte
[md5] 89930ad5cf340af7a101ed74261d4a34
修正
sendOAuthRequest()メソッドでHTTP_OAuth_Exceptionがthrowされる問題
serchメソッドでhttp://search.twitter.comを利用しない問題
blocks/createメソッドでuser_id、screen_nameを指定できない問題
blocks/destroyメソッドでuser_id、screen_nameを指定できない問題
テストでOAuthの場合sourceはアプリケーションになる問題
テストでutf8テストが二重投稿になる問題
テストでOAuthの場合オブジェクトがHTTP_OAuth_Consumer_Responseになる問題
追加
blocks/bloking/idsメソッド (call $t->blocks->blocking_ids())
$ svn export -r 302262 http://svn.php.net/repository/pear/packages/Services_Twitter/trunk Services_Twitter $ wget http://blog.cheki.net/Services_Twitter_r302262.diff $ cd Services_Twitter $ patch -p0 < ../services_twitter.diff patching file Services/Twitter.php patching file data/api.xml patching file tests/900-exceptions.phpt patching file tests/910-options.phpt patching file tests/920-utf8.phpt patching file tests/data/utf8-2.dat patching file tests/setup.php
Services_Twitter
以前パッチが取り込まれたけど、Basic認証でしか確認してなくて、OAuthだとDELETEメソッドで問題がでてました。
その他なんか迷走してる感じだったので、パッチ送りたいなとPEARアカウントを取得しようと申請したのですがレスポンスがないので、とりあえずgithubにぶち込んだ次第です。
バグレポートもパッチ投げるにもPEARアカウントないと半匿名な感じになるので。
cockok's Services_Twitter at master – GitHub
リリース最新は0.6.2ですが、trunkが変更されてたのでそちらをfork。
あいきゃんとすぴーくいんぐりっしゅ。
もちろん書くのも。
ちなみに、DELETEメソッドででる問題は、RFC2616に以下のようなところ。
The DELETE method requests that the origin server delete the resource identified by the Request-URI.
HTTP/1.1: Method Definitions
こっちはPEAR::HTTP_OAuthの問題。
Manual :: 共有ホストでの PEAR のローカルコピーのインストール
既にpearコマンドが利用可能な場合。
$ pear config-create /path/to ~/.pearrc
/path/to/pear以下に諸々配置される設定ファイルが~/.pearrcとして作成される。
この場合、PEARディレクトリは/path/to/pear/phpとなる。
ディレクトリ等、設定をを変更したい場合は、通常通りpear config-setコマンドで。
phpコマンドを実行する際にinclude_pathの設定をしたいが、オプションでは指定できないので、php.iniを別途準備し読むように指定するaliasを設定。
$ cp /etc/php.ini /path/to $ vim /path/to/php.ini $ alias php='php -c /path/to/php.ini'
php.iniのinclude_pathの設定は以下のようにローカルのPEARディレクトリしか読み込まない、または、優先的に読み込むように設定。
include_path = ".:/path/to/php" #include_path = ".:/path/to/php:/usr/share/pear:/usr/share/php"
まさに誰得。
ブラウザベースは情報いっぱいあったんだけど、クライアントベースが見つからなかったので。
取得したaccess_token、access_token_secretを利用の仕方は下記で。
PEAR::HTTP_OAuthでxAuth – ぱんぴーまっしぐら
#!/usr/bin/env php
<?php
require_once 'HTTP/OAuth/Consumer.php';
define('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxx');
define('CONSUMER_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
try {
$http_request = new HTTP_Request2();
$http_request->setConfig('ssl_verify_peer', false);
$consumer_request = new HTTP_OAuth_Consumer_Request();
$consumer_request->accept($http_request);
$consumer = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET);
$consumer->accept($consumer_request);
$consumer->getRequestToken('http://twitter.com/oauth/request_token');
$url = $consumer->getAuthorizeUrl('http://twitter.com/oauth/authorize');
if (!$stdin = fopen('php://stdin', 'r')) {
throw new Exception('stdin open failed');
}
echo 'Visit the following URL in your browser to authenticate twitter: ';
echo $url."\n";
echo 'Enter Twitter OAuth PIN: ';
$pin = trim(fgets($stdin, 64));
$consumer->getAccessToken('http://twitter.com/oauth/access_token', $pin);
echo 'access_token: '.$consumer->getToken()."\n";
echo 'access_token_secret: '.$consumer->getTokenSecret()."\n";
} catch (Exception $e) {
echo $e->getMessage().' in '.$e->getFile().' line '.$e->getLine()."\n";
exit(1);
}
# yum --enablerepo=remi,epel install php-pear-PHP-CodeSniffer
$ phpcs hoge.php
Services_Twitter::sendOAuthRequestではHTTP_Request2_Exceptionをcatchしていますが、実際はHTTP_OAuth_Exceptionをcatchしなきゃいけない。
Services_Twitterはベーシック認証周りは大丈夫だと思いますが、OAuth周りはまともにテストされてないので、結構バグあります。