清单 6. getUserTimeline() 方法的输出
<rss version="2.0">
<channel>
<title>Twitter / myerman</title>
<link>http://twitter.com/myerman</link>
<description>Twitter updates from Thomas Myer / myerman.</description>
<language>en-us</language>
<ttl>40</ttl>
<item>
<title>myerman: just got my copy of groundswell...also,
just got back from seeing Taken. Liam Neeson kicks butt!</title>
<description>myerman: just got my copy of groundswell...also, just got back from
seeing Taken. Liam Neeson kicks butt!</description>
<pubDate>Sun, 01 Feb 2009 04:16:55 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1166126064</guid>
<link>http://twitter.com/myerman/statuses/1166126064</link>
</item>
<item>
<title>myerman: For those of us trying to learn
Objective-C - some libraries. http://tinyurl.com/dkaj4m</title>
<description>myerman: For those of us trying to learn
Objective-C - some libraries. http://tinyurl.com/dkaj4m</description>
<pubDate>Fri, 30 Jan 2009 20:06:13 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1162686918</guid>
<link>http://twitter.com/myerman/statuses/1162686918</link>
</item>
<item>
<title>myerman: RT @andyhunter Google Chief Economist
on innovation: We’re in the middle of .. a period of “combinatorial innovation.”
http://bit.ly/xgpN</title>
<description>myerman: RT @andyhunter Google Chief Economist
on innovation: We’re in the middle of .. a period of “combinatorial innovation.”
http://bit.ly/xgpN</description>
<pubDate>Fri, 30 Jan 2009 15:35:18 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1161902779</guid>
<link>http://twitter.com/myerman/statuses/1161902779</link>
</item>
<item>
<title>myerman: A-Team movie out next year? I love it when a plan comes together.
http://tinyurl.com/ahckx9</title>
<description>myerman: A-Team movie out next
year? I love it when a plan comes together.
http://tinyurl.com/ahckx9</description>
<pubDate>Thu, 29 Jan 2009 19:21:34 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1159386717</guid>
<link>http://twitter.com/myerman/statuses/1159386717</link>
</item>
<item>
<title>myerman: @andyhunter @cesart it's the pulvinar that gets you in trouble...
every time!</title>
<description>myerman: @andyhunter @cesart
it's the pulvinar that gets you in trouble...
every time!</description>
<pubDate>Thu, 29 Jan 2009 16:44:30 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1158923928</guid>
<link>http://twitter.com/myerman/statuses/1158923928</link>
</item>
<item>
<title>myerman: Create a manga avatar of yourself. http://www.faceyourmanga.com
</title>
<description>myerman: Create a manga avatar of yourself. http://www.faceyourmanga.com
</description>
<pubDate>Thu, 29 Jan 2009 16:43:55 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1158922223</guid>
<link>http://twitter.com/myerman/statuses/1158922223</link>
</item>
<item>
<title>myerman: Zombies ahead on Lamar & 15th....silly hackers!
http://tinyurl.com/c2s3nw</title>
<description>myerman: Zombies ahead on Lamar & 15th....silly hackers!
http://tinyurl.com/c2s3nw</description>
<pubDate>Thu, 29 Jan 2009 13:21:24 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1158382096</guid>
<link>http://twitter.com/myerman/statuses/1158382096</link>
</item>
<item>
<title>myerman: Obama's Mac: http://tinyurl.com/b4hsza</title>
<description>myerman: Obama's Mac: http://tinyurl.com/b4hsza
</description>
<pubDate>Thu, 29 Jan 2009 13:17:58 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1158375012</guid>
<link>http://twitter.com/myerman/statuses/1158375012</link>
</item>
<item>
<title>myerman: OK, looks like I'll be
speaking on Saturday at SxSW 2009. More details
for the freelancer in your life: http://tinyurl.com/df8my9</title>
<description>myerman: OK, looks like I'll be speaking on Saturday at SxSW 2009.
More details for the freelancer in your life: http://tinyurl.com/df8my9</description>
<pubDate>Wed, 28 Jan 2009 22:39:29 +0000</pubDate>
<guid>http://twitter.com/myerman/statuses/1156744158</guid>
<link>http://twitter.com/myerman/statuses/1156744158</link>
</item>
...snip
</channel>
</rss>
有一点要注意:在达到速率限制之前,您不会只想重复调用这个函数。相反,您需要稍微控制一下请求。使用 rateLimitStatus() 方法和一些自己定制的(home-rolled)缓存可以做一些更聪明的事情,以实现本地缓存效果。
如清单 7 所示,rateLimitStatus() 方法是一个简单的请求,您可以使用它来检查您是否已经达到了速率限制 — 检查速率限制状态不会对您不利。您所要做的只是提供一个格式,该函数会完成所有其他事情。
清单 7. rateLimitStatus() 方法
$user = "your-username";
$pw = "your-pw";
$twitter = new Twitter($user,$pw);
$status = $twitter->rateLimitStatus("xml");
echo $status;
运行 rateLimitStatus() 方法时,会返回以下 XML。
清单 8. rateLimitStatus() 方法的输出
<hash>
<hourly-limit type="integer">100</hourly-limit>
<reset-time type="datetime">2009-02-02T05:44:45+00:00</reset-time>
<reset-time-in-seconds type="integer">1233553485</reset-time-in-seconds>
<remaining-hits type="integer">99</remaining-hits>
</hash>
您需要的是 remaining-hits 节点内的值。如果该值达到 5 左右(或者可能是 10),就转到缓存的 RSS 提要。有了这个 XML 节点集后,您所要做的就是加载 SimpleXML 来快速解析速率限制状态。当然,如果您不熟悉 XML 的用法,那么最好解析为 JSON。
使用 JSON 格式运行相同的代码将得到精简得多的语句:
"reset_time_in_seconds":1233553485}
花些时间用逗号将这个字符串分开,然后再用冒号获取 remaining_hits 的值。当然这很简单。但是,如果您运行的是 PHP V5.2.0 或更高版本,可能已经能使用 json_decode() 方法了。如清单 9 所示,您所要做的只是使用该函数得到 remaining_hits 数字,然后使用一个简单的 if 语句看看该数字是接近于 10 还是更小。如果不是,从 Twitter 拖出 RSS 提要,不过将它保存到一个文件供稍后使用。如果小于 10,就使用本地缓存的保存后的 RSS 文件。
清单 9. 使用 json_decode() 方法
$user = "your-username";
$pw = "your-password";
$twitter = new Twitter($user,$pw);
$status = $twitter->rateLimitStatus("json");
$fileName = $user.".rss";
$json_dump = json_decode($status);
$remaining = $json_dump->remaining_hits;
if ($remaining <= 10){
$fh = fopen($fileName, 'r');
$rssData = fread($fh, filesize($fileName));
fclose($fh);
echo $rssData;
}else{
$rss = $twitter->getUserTimeline("rss", $user);
//write to file
//in case we need it!
$fh = fopen($fileName,'w') or die("can't write to file");
fwrite($fh,$rss);
fclose($fh);
echo $rss;
}
剩下要做的是为 PHP 文件指定一个合适的名称(例如,twitterfeed.php),然后合理使用它,即将它作为另一个订阅选项提供给用户,提供 WordPress 侧栏小部件或诸如此类的任何对象。