|

這個(gè)欄目最開(kāi)始調(diào)用微博飯否的API來(lái)做的,因?yàn)楸娝苤木壒剩埛駸o(wú)法使用了,后來(lái)采用騰訊的滔滔API來(lái)實(shí)現(xiàn).2010年1月26日滔滔業(yè)務(wù)將會(huì)開(kāi)始和QQ空間心情整合,只能考慮放棄。思來(lái)想去,最終還是考慮用Twitter來(lái)實(shí)現(xiàn),不過(guò)Twitter在國(guó)內(nèi)無(wú)法訪問(wèn),不能采用js的方式來(lái)調(diào)用。本博客的服務(wù)器才國(guó)外,用php的方式訪問(wèn)Twitter的API應(yīng)該沒(méi)有問(wèn)題,雖然有現(xiàn)成的wordpress插件“Twitter Tools”可以用,但本著盡量少用插件的目的,決定直接用php在wordpress主題里實(shí)現(xiàn)。twritter提供的API接口很豐富,研究一下覺(jué)得調(diào)用Twitter RSS的API比較簡(jiǎn)單,實(shí)現(xiàn)如下功能:
1、抓取twitter RSS的內(nèi)容,不需要密碼,只需要用戶名。
2、格式化RSS的內(nèi)容,顯示用戶本人的推的內(nèi)容及時(shí)間,排除 @replies 回復(fù)給他人的信息內(nèi)容。
代碼如下:
復(fù)制代碼 代碼如下:
<!-- my tritter -->
<?php
$username='xjb';//change this to your twitter username修改為你的twitter 用戶名
$feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss';
$excludePattern='/'.$username.': @/'; //excludes any @replies排除@replies 內(nèi)容
$count=5;// show count
$i=0;
if(!$xml=simplexml_load_file($feedURL)){
trigger_error('Error',E_USER_ERROR);
}
foreach($xml->channel->item as $item) {
if ( ! preg_match("$excludePattern", $item->title)) {
$filteredTitle=htmlspecialchars("$item->title");
$filteredTitle=str_replace("$username: ","",$filteredTitle);
//Convert the time zone in China --轉(zhuǎn)成中國(guó)時(shí)區(qū)
date_default_timezone_set('Asia/Shanghai');
$i++;
if($i>$count)
{
break;
}
?>
<li><?php echo $filteredTitle; ?>
(<?php echo date("Y-m-d H:i:s",strtotime($item->pubDate)); ?>)</li>
<?php } } ?>
<div align="right">
<a target="_blank">更多...</a></div>
<!-- my tritter -->
源代碼
復(fù)制代碼 代碼如下:
<!-- my tritter -->
<?php
$username='xjb'; //change this to your twitter username --修改為你的twitter 用戶名
$feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss';
$excludePattern='/'.$username.': @/'; //excludes any @replies --排除 @replies 內(nèi)容
$count=5;// show count
$i=0;
if(!$xml=simplexml_load_file($feedURL)){
trigger_error('Error',E_USER_ERROR);
}
foreach($xml->channel->item as $item) {
if ( ! preg_match("$excludePattern", $item->title)) {
$filteredTitle=htmlspecialchars("$item->title");
$filteredTitle=str_replace("$username: ","",$filteredTitle);
date_default_timezone_set('Asia/Shanghai'); //Convert the time zone in China --轉(zhuǎn)成中國(guó)時(shí)區(qū)
$i++;
if($i>$count)
{
break;
}
?>
<li><?php echo $filteredTitle; ?>(<?php echo date("Y-m-d H:i:s",strtotime($item->pubDate)); ?>)</li>
<?php } } ?>
<div align="right"><a target="_blank">更多...</a></div>
<!-- my tritter -->
php技術(shù):PHP調(diào)用Twitter的RSS的實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。