微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP调用Twitter的RSS的实现代码

杂感


这个栏目最开始调用微博饭否的API来做的,因为众所周知的缘故,饭否无法使用了,后来采用腾讯的滔滔API来实现.2010年1月26日滔滔业务将会开始和QQ空间心情整合,只能考虑放弃。思来想去,最终还是考虑用Twitter来实现,不过Twitter在国内无法访问,不能采用js的方式来调用。本博客的服务器才国外,用PHP的方式访问Twitter的API应该没有问题,虽然有现成的wordpress插件“Twitter Tools”可以用,但本着尽量少用插件的目的,决定直接用PHPwordpress主题里实现。twritter提供的API接口很丰富,研究一下觉得调用Twitter RSS的API比较简单,实现如下功能: 1、抓取twitter RSS内容,不需要密码,只需要用户名
2、格式化RSS内容显示用户本人的推的内容及时间,排除 @replies 回复给他人的信息内容代码如下:
代码如下:

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;
}
?>

  • (pubDate)); ?>)





  • 源代码
    代码如下:

    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',$filteredTitle);
    date_default_timezone_set('Asia/Shanghai'); //Convert the time zone in China --转成中国时区
    $i++; if($i>$count)
    {
    break;
    }
    ?>
  • (pubDate)); ?>)




  • 原文地址:https://www.jb51.cc/php/28728.html

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

    相关推荐