$num_of_tweets = 30;
$user_name = "your_name";
$contents = file_get_contents('http://search.twitter.com/search.atom?q=from:'.$user_name.'&rpp='.$num_of_tweets);
$xml = simplexml_load_string($contents);
print ''.$user_name.'
';
print '';
for ($i = 0; $i < $num_of_tweets; $i++ ) {
$tweet = $xml->entry[$i]->title;
$tweet = mb_convert_encoding($tweet,"SJIS","UTF-8");
$date_tweeted = $xml->entry[$i]->published;
$date_tweeted = date('Y/m/d H:i:s',strtotime($date_tweeted));
print $tweet.' ('.$date_tweeted.')
';
}
print '
';
件数を100件にしたが、うまくいかなかった。
phpでoauthを使う場合の定番とされているものがあるようだ。
https://github.com/abraham/twitteroauth
OAuth.php
twitteroauth.php
この二つのファイルをダウンロードしておく。
下記がサンプル。上記二つのファイルはこのスクリプト自体と同じ場所においておく。
require_once('twitteroauth.php');
$consumer_key = 'xxxxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxx';
$oauth_token = 'xxxxxxxxxxxxxxxxxxxx';
$oauth_secret = 'xxxxxxxxxxxxxxxxxxxxx';
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_secret);
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => date(DATE_RFC822)));
実行しても何も表示されないが、twitterの自分のページを見ると、日付と時刻がtweetされているだろう。
今は自分のアプリケーションで自分のアカウントでツイートしているから、スクリプトに自分のconsumer key/secret, oauth token/secretを書き込んでいるが、実際には oauth token/secretは、利用者のそれを取得する必要がある。そのやり方も公開している人がいて、今ためしたらoauthによる認証まではできているようなのだが、肝心のそのあとのAPI操作ができない・・・
session_start();
require_once('twitteroauth.php');
$consumer_key = 'xxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxx';
$state = $_SESSION['oauth_state'];
$session_token = $_SESSION['oauth_request_token'];
$oauth_token = $_REQUEST['oauth_token'];
$section = $_REQUEST['section'];
if ($_REQUEST['test'] === 'clear') {
session_destroy();
session_start();
}
if ($_REQUEST['oauth_token'] != NULL && $_SESSION['oauth_state'] === 'start') {
$_SESSION['oauth_state'] = $state = 'returned';
}
switch ($state) {
default:
$to = new TwitterOAuth($consumer_key, $consumer_secret);
$tok = $to->getRequestToken();
$_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
$_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
$_SESSION['oauth_state'] = "start";
$request_link = $to->getAuthorizeURL($token);
$content = 'Click on the link to go to twitter to authorize your account.';
$content .= '
'.$request_link.'';
//(A)
$req = $to->OAuthRequest("https://twitter.com/statuses/update.xml","POST",array("status"=>"test"));
header("Location: $request_link");
break;
case 'returned':
if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) {
$to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_request_token'],$_SESSION['oauth_request_token_secret']);
$tok = $to->getAccessToken();
$_SESSION['oauth_access_token'] = $tok['oauth_token'];
$_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];
//(B)
$req = $to->OAuthRequest("https://twitter.com/statuses/update.xml","POST",array("status"=>"test"));
}
$_SESSION['username'] = $tok["screen_name"];
//(C)
$req = $to->OAuthRequest("https://twitter.com/statuses/update.xml","POST",array("status"=>"test"));
header("Location: /");
}
最初は(C)のところだろうと書いてみたが、internal server errorになる。
(A),(B)に書いてみるとエラーにならないがtweetもされない。