PHP调用Twitter的RSS的实现代码

2015-01-24信息快讯网

“守望轩”博客右侧边栏原来有个“杂感”的栏目,用来记录短的、不能大篇幅成文的短句,或者自己比较喜欢的短句和言论。


这个栏目最开始调用微博饭否的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 -->
关于php连接mssql:pdo odbc sql server
PHP中date()日期函数有关参数整理
在WAMP环境下搭建ZendDebugger php调试工具的方法
php自定义函数call_user_func和call_user_func_array详解
php array_intersect比array_diff快(附详细的使用说明)
php header Content-Type类型小结
php中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL报错解决方法
php设计模式 Visitor 访问者模式
php设计模式 Adapter(适配器模式)
php设计模式 Interpreter(解释器模式)
php设计模式 Composite (组合模式)
php设计模式 Chain Of Responsibility (职责链模式)
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
了解Joomla 这款来自国外的php网站管理系统
PHP中include()与require()的区别说明
PHP set_time_limit(0)长连接的实现分析
PHP正则的Unknown Modifier错误解决方法
Search File Contents PHP 搜索目录文本内容的代码
php+mysql事务rollback&commit示例
php date与gmdate的获取日期的区别
PHPMailer 中文使用说明小结
PHP has encountered an Access Violation 错误的解决方法
php jquery 实现新闻标签分类与无刷新分页
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
php session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
PHP CKEditor 上传图片实现代码
php getsiteurl()函数
PHP has encountered an Access Violation at 7C94BD02解决方法
php ignore_user_abort与register_shutdown_function 使用方法
UCenter Home二次开发指南
©2014-2024 dbsqp.com