我在网站上有一个 PHP 脚本,它可以提取 Twitter 提要并显示它。奇怪的是,大多数时候它似乎工作得很好,但有时(实际上很多)它根本不起作用,只是显示跟随按钮。
代码如下,明显USERNAME
有实际的twitter账号用户名在:
$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 />\n";
if ($i++ >= $max_tweets)
break;
}
echo "</ul>\n";
}
// 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>";
}