dpy83214 2012-10-12 16:41
浏览 11
已采纳

PHP Twitter脚本并不总是有效

I have a PHP script on the site that pulls a twitter feed and displays it. Strangely most of the time it seems to work just fine, but sometimes (quite a lot actually) it doesn't work at all and just displays the follow button.

The code is as follows, obviously USERNAME has the actual twitter account username in:

$widget = true;
$twitterid = "@USERNAME";

$doc = new DOMDocument();

# load the RSS document, edit this line to include your username or user id
if($doc->load('http://twitter.com/statuses/user_timeline/USERNAME.rss')) {

    # specify the number of tweets to display, max is 20
    $max_tweets = 4;    

    $i = 1;
    foreach ($doc->getElementsByTagName('item') as $node) {
        # fetch the title from the RSS feed. 
        # Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
        $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

        # the title of each tweet starts with "username: " which I want to remove
        $tweet = substr($tweet, stripos($tweet, ':') + 1);   

        # OPTIONAL: turn URLs into links
        $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);

        # OPTIONAL: turn @replies into links
        $tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

        echo "<p> <p>".$tweet."</p></p><hr />
";

        if ($i++ >= $max_tweets)
            break;
    }
    echo "</ul>
";
} 

// Here's the Twitter Follow Button Widget
if($widget){
    echo "<a href=\"https://twitter.com/" .$twitterid. "\" class=\"twitter-follow-button\" data-show-count=\"true\">Follow @" .$twitterid. "</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
}
  • 写回答

3条回答 默认 最新

  • douyiyang6317 2012-10-13 11:58
    关注

    Sadly Twitter has removed the URL https://twitter.com/statuses/user_timeline/USERNAME.rss and it now returns Sorry, that page does not exist as of Oct 12 2012. There is a json equivalent however this may fail as well after March 2013. Try https://api.twitter.com/1/statuses/user_timeline.json?screen_name=USERNAME&count=4 for the time being.

    HTH

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能