程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP調用Twitter的RSS的實現代碼

PHP調用Twitter的RSS的實現代碼

編輯:關於PHP編程

雜感
這個欄目最開始調用微博飯否的API來做的,因為眾所周知的緣故,飯否無法使用了,後來采用騰訊的滔滔API來實現.2010年1月26日滔滔業務將會開始和QQ空間心情整合,只能考慮放棄。思來想去,最終還是考慮用Twitter來實現,不過Twitter在國內無法訪問,不能采用js的方式來調用。本博客的服務器才國外,用php的方式訪問Twitter的API應該沒有問題,雖然有現成的wordpress插件“Twitter Tools”可以用,但本著盡量少用插件的目的,決定直接用php在wordpress主題裡實現。twritter提供的API接口很豐富,研究一下覺得調用Twitter RSS的API比較簡單,實現如下功能:

1、抓取twitter RSS的內容,不需要密碼,只需要用戶名。
2、格式化RSS的內容,顯示用戶本人的推的內容及時間,排除 @replies 回復給他人的信息內容。

代碼如下:
復制代碼 代碼如下:
<!-- 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 內容
$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 --轉成中國時區
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 href="http://twitter.com/xjb" 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 內容
$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 --轉成中國時區
$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 href="http://twitter.com/xjb" target="_blank">更多...</a></div>
<!-- my tritter -->

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved