Services_Twitterを使ってみる。

なんだかんだでこれからTwitterぐらいは対応させないとダメだよねーという妄想で
これから始まるPHP制作にTwitterの自分の最新更新のつぶやきを反映させたいなぁと。
ただ、表示するだけなんですけどね。
どうすればいいだろうなーと漠然と思っていたら
Services_Twitterというステキなパッケージがあるらしい。
ということで試してみた。

まずはインストール

sudo pear install -f http://labs.transrain.net/files/Services_Twitter-0.4.0.tgz

これだけで終了。ああ、PEARって便利ですね。

まずは書いてみる。

公式サイトを参考にしつつ書いてみる。
今回は自分の最新の行さえ読めればいいのでそれだけを実装。

<?php
require_once "Services/Twitter.php";

$user = 'hogehoge'; // TwitterのID
$pass = 'fugafuga'; // Twitterのパスワード

$st = new Services_Twitter($user,$pass);

var_dump($st->getUserTimeline());

まずはどういうデータになっているのかvar_dumpで見てみましょう。

訳わかんないデータが><
Services_Twitterのサイトを読むとTwitterからはJSONという文字列で提供されるらしい。

じゃあ、JSONをどうにかしなくちゃ

ということで調べたら
PHP: json_decode - Manual
PHPに用意されていた。ステキ><
ということで直す。

var_dump(json_decode($st->getUserTimeline()));


こうなればどうにかなりますね。

あとは最新を取り出す。

$status = json_decode($st->getUserTimeline());
echo $status[0]->text;


これで取り出せた。

なんか、変なvar_dumpの結果が混じっていたら?

参考:http://dreaminess.info/2009/02/services-twitter.html

どうしたものかと思ってとりあえずServices/Twitter.phpで出力してそうな所を探す事数分

501 var_dump($line);

なにこれワロス
削除して解決したのでした。

とあったのでTwitter.phpの501行目にあるvar_dump($line)をコメントアウトして完了。

どこまでできるかな?

以下は実際のデータ。
結構取れるのね。すげー。

object(stdClass)#2 (10) {
  ["truncated"]=>
  bool(false)
  ["text"]=>
  string(18) "ほげーほげー"
  ["user"]=>
  object(stdClass)#3 (25) {
    ["following"]=>
    NULL
    ["friends_count"]=>
    int(0)
    ["profile_sidebar_border_color"]=>
    string(6) "87bc44"
    ["description"]=>
    NULL
    ["time_zone"]=>
    NULL
    ["utc_offset"]=>
    NULL
    ["profile_text_color"]=>
    string(6) "000000"
    ["profile_image_url"]=>
    string(59) "http://static.twitter.com/images/default_profile_normal.png"
    ["profile_background_image_url"]=>
    string(53) "http://static.twitter.com/images/themes/theme1/bg.gif"
    ["created_at"]=>
    string(30) "Thu Jul 09 07:25:35 +0000 2009"
    ["url"]=>
    NULL
    ["name"]=>
    string(6) "hokima"
    ["statuses_count"]=>
    int(1)
    ["profile_link_color"]=>
    string(6) "0000ff"
    ["protected"]=>
    bool(false)
    ["screen_name"]=>
    string(6) "hokima"
    ["notifications"]=>
    NULL
    ["profile_background_tile"]=>
    bool(false)
    ["favourites_count"]=>
    int(0)
    ["profile_background_color"]=>
    string(6) "9ae4e8"
    ["verified"]=>
    bool(false)
    ["profile_sidebar_fill_color"]=>
    string(6) "e0ff92"
    ["followers_count"]=>
    int(0)
    ["location"]=>
    NULL
    ["id"]=>
    int(55172134)
  }
  ["in_reply_to_status_id"]=>
  NULL
  ["in_reply_to_user_id"]=>
  NULL
  ["created_at"]=>
  string(30) "Thu Jul 09 07:26:22 +0000 2009"
  ["favorited"]=>
  bool(false)
  ["in_reply_to_screen_name"]=>
  NULL
  ["id"]=>
  float(2547047696)
  ["source"]=>
  string(3) "web"
}
これらを組み合わせて適当に作ってみた。


これだけでいいのね。便利><

<?php
require_once "Services/Twitter.php";

$user = '';
$pass = '';

$st = new Services_Twitter($user,$pass);

$status = json_decode($st->getUserTimeline());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<title><?php echo $status[0]->user->name ?></title>
<style type="text/css">
  body {
    background: url(<?php echo $status[0]->user->profile_background_image_url ?>);
  }
</style>
</head>
<body>
<p><img src="<?php echo $status[0]->user->profile_image_url ?>"></p>
<h1><?php echo $status[0]->user->name ?></h1>
<h2><?php echo $status[0]->text ?></h2>
<p><?php echo $status[0]->user->created_at ?></p>

</body>
</html>