PHP調(diào)用Twitter的RSS的實現(xiàn)代碼
更新時間:2010年03月10日 22:08:11 作者:
“守望軒”博客右側(cè)邊欄原來有個“雜感”的欄目,用來記錄短的、不能大篇幅成文的短句,或者自己比較喜歡的短句和言論。

這個欄目最開始調(diào)用微博飯否的API來做的,因為眾所周知的緣故,飯否無法使用了,后來采用騰訊的滔滔API來實現(xiàn).2010年1月26日滔滔業(yè)務將會開始和QQ空間心情整合,只能考慮放棄。思來想去,最終還是考慮用Twitter來實現(xiàn),不過Twitter在國內(nèi)無法訪問,不能采用js的方式來調(diào)用。本博客的服務器才國外,用php的方式訪問Twitter的API應該沒有問題,雖然有現(xiàn)成的wordpress插件“Twitter Tools”可以用,但本著盡量少用插件的目的,決定直接用php在wordpress主題里實現(xiàn)。twritter提供的API接口很豐富,研究一下覺得調(diào)用Twitter RSS的API比較簡單,實現(xiàn)如下功能:
1、抓取twitter RSS的內(nèi)容,不需要密碼,只需要用戶名。
2、格式化RSS的內(nèi)容,顯示用戶本人的推的內(nèi)容及時間,排除 @replies 回復給他人的信息內(nèi)容。
代碼如下:
復制代碼 代碼如下:
<!-- 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)成中國時區(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 -->
源代碼
復制代碼 代碼如下:
<!-- 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)成中國時區(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 -->
相關(guān)文章
php和javascript之間變量的傳遞實現(xiàn)代碼
本文提供一種解決php和javascript之間變量的傳遞的方法,需要的朋友可以參考下2012-12-12PHP遍歷某個目錄下的所有文件和子文件夾的實現(xiàn)代碼
本篇文章是對PHP遍歷某個目錄下的所有文件和子文件夾的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-06-06php array_map使用自定義的函數(shù)處理數(shù)組中的每個值
這篇文章主要介紹了php array_map使用自定義的函數(shù)處理數(shù)組中的每個值的相關(guān)資料,需要的朋友可以參考下2016-10-10PHP中數(shù)字檢測is_numeric與ctype_digit的區(qū)別介紹
PHP中的兩個函數(shù)is_numeric和ctype_digit都是檢測字符串是否是數(shù)字,但也存在一點區(qū)別2012-10-10WordPress的文章自動添加關(guān)鍵詞及關(guān)鍵詞的SEO優(yōu)化
這篇文章主要介紹了給WordPress的文章添加關(guān)鍵詞及關(guān)鍵詞的SEO優(yōu)化方法,突出關(guān)鍵詞在搜尋結(jié)果中的作用,需要的朋友可以參考下2016-03-03php中運用http調(diào)用的GET和POST方法示例
調(diào)用的GET和POST方法,使用到的函數(shù)是curl_init, curl_setopt, curl_exec,curl_close,默認是GET方法2014-09-09PHP iconv 解決utf-8和gb2312編碼轉(zhuǎn)換問題
就一個很簡單的函數(shù)iconv();但是就是這個函數(shù)在網(wǎng)上找了很多例子,都無法成功轉(zhuǎn)換,這是為什么呢?2010-04-04