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>";
}
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
Twitter enforces rate limiting on unauthenticated calls (calls made to the API that haven't been authenticated using OAuth).
"Unauthenticated calls are permitted 150 requests per hour. Unauthenticated calls are measured against the public facing IP of the server or device making the request."
If you are using shared hosting, it makes it more likely for you to get rate-limited as someone else using the same IP on the host could also be querying the Twitter API (hence, counting towards the hourly limit for that IP).
You can read more on these restrictions on Twitter's Rate Limiting restrictions website as well as on the Rate Limiting FAQ website.
<?php
$timeline="http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=arvizard";
$xml= new SimpleXMLElement(file_get_contents($timeline));
$i=0;
print "<ul class=\"tweet_list\">";
foreach($xml ->children() as $tstatus)
{
$stat=$tstatus->text;
$split= preg_split('/\s/',$stat);
print "<li class=\"tweet\"><p class=\"tweet_text\">";
foreach ($split as $word)
{
if (preg_match('/^@/',$word)) {
print " "."<a href=http://www.twitter.com/".substr($word,1).">".$word."</a>";
}
else if (preg_match('/^http:\/\//',$word)){
print " "."<a href=".$word.">".$word."</a>";
}
else
{
print " ".$word;
}
}
print "</p>";
print "<span class=\"date\">".substr($tstatus->created_at,0,strlen($tstatus->created_at)-14)."</span>";
print "</li>";
$i++;
if ($i==5)
{
break;
}
}
print "</ul>";
?>
This may help you. please check it.