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>

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

Tagged with:

Recent Entries

コメントをどうぞ