friendships/exists Twitter API サンプル

friendships/existsのサンプルを作ってみた。
このメソッドは指定した2ユーザの間の friend 関係を調べる。

ってことで早速元ライブドア社長とのfriend 関係を調べるサンプルを
作ってみた。

<?php

$person1 = ‘AP_chan’;
$person2 = ‘takapon_jp’;

$url = ‘http://twitter.com/friendships/exists’;
$format = ‘xml’;

$persons12 = make_request($url.’.’.$format.’?user_a=’.$person1.’&user_b=’.$person2);
$result = get_match(’/<friends>(.*)<\/friends>/isU’,$persons12);
echo ‘<p>私達<b>’.$person1.’</b>と<b>’.$person2.’</b>は友達ですか?’.’ | 答えは・・・<b>’.$result.’</b></p>’;

function make_request($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/* gets the match */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}

?>

こんな感じで、簡単に表示できた。

デモ はこちら

参照はコチラ

PHPとTwitter apiでアプリを作ってみる。

まずはTwitterへの登録が必要みたい。
※APIを使用するにはメールアドレスとパスワードが必要。

早速 サンプル用Twitterを登録してみる。

まずは簡単に表示できるかチェックしてみようってことで、
Twitter 仕様書を参考にPHPで作ってみた。

Twitter APIのサンプル

サンプルコードはコチラから

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>Twitter API サンプル</title>
</head>
<body>
<h1>Twitter API サンプル</h1>
<?php

//ユーザーIDとパスワードの設定
$email = “登録メールアドレス“;
$password = “登録パスワード“;

$host = “http://twitter.com/statuses/public_timeline.xml”;
$host .= “?since=”.urlencode(date(’D, d M Y G:i:s GMT’, strtotime(’-10 day’)));
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, “$email:$password”);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

$result = curl_exec($ch);
curl_close($ch);
$XML = simplexml_load_string($result);

echo “<ul>”;
foreach ($XML->status as $status){
echo “<li><b style=\”color:red;\”>”.$status->user->screen_name.”</b>: “.$status->text.”</li><hr>\n”;
}
echo “</ul>”;

?>
<p>powerd by <a href=”http://apiwiki.twitter.com/”>Twitter API</a></p>
</body>

</html>

こんな感じで、公開している名前とツイートを簡単に表示できた。
最新の表示はコチラ