利用twitter search API制作博客挂件

这是最近我在网上偶然获得的一段PHP代码,感觉比官方的Twitter badges更具实用性和灵活性。所以特地在博客中分享一下。

众所周知,Twitter自从收购了Summize以后。Twitter search就变得更健壮和完美了,并且,所有的搜索结果都可以输出为RSS订阅源。灵活输出RSS FEED这是一项非常重要的特性。我们可以借此定制各种各样的查询条件,并利用其RSS输出到任何地方。特别值得一提的是,Twitter search的RSS输出刷新速度极快,据我的个人体验,几乎达到了实时的地步。

OK,让我们步入正题,利用Twitter search API制作博客挂件的PHP代码如下:
<?php
// Your twitter username.
$username = " ";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
// Suffix - some text you want display after your latest tweet. (Same rules as
the prefix.)
$suffix = "";
$feed = "http://twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$stepthree = explode("<updated>",$feed);
$stepfour = explode("</updated>",$stepthree[1]);
$stepfive = explode("T",$stepfour[0]);
$tweet = $stepTwo[0]."--(".$stepfive[0].")";
$tweet = str_replace("&lt;", "<", $tweet);
$tweet = str_replace("&gt;", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>

鉴于目前Twitter官网已经无法访问,请将此代码中的twitter.com换成第三方Twitter search API地址 [注意:有些Twitter API没有search功能,请确定你的API支持search]。如果你对PHP小有研究,也可以根据自己的设想修改上面这段代码使输出更符合个人愿望。
Enjoy!!

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • Haohao
  • LinkedIn
  • MisterWong
  • MSN Reporter
  • Yahoo! Bookmarks
  • 豆瓣
  • 豆瓣九点
  • Facebook
  • QQ书签
  • Twitter

关键词:

One Response to “利用twitter search API制作博客挂件”

  1. 开心凡人 says:

    今天有空到博主这来看看

Leave a Reply

Follow Me On Twitter