/ Published in: PHP
Some PHP code that can be used to get formatted HTML containing N number of tweets for a given twitter user.
Expand |
Embed | Plain Text
<?php /* Description: Twitter PHP code Author: Andrew MacBean Version: 1.0.0 */ /** Method to make twitter api call for the users timeline in XML */ function twitter_status($twitter_id) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml"); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($c, CURLOPT_TIMEOUT, 5); $response = curl_exec($c); $responseInfo = curl_getinfo($c); curl_close($c); $xml = new SimpleXMLElement($response); return $xml; } else { return $response; } } else { return false; } } /** Method to add hyperlink html tags to any urls, twitter ids or hashtags in the tweet */ function processLinks($text) { $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text ); $text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text); $text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text); return $text; } /** Main method to retrieve the tweets and return html for display */ function get_tweets($twitter_id, $nooftweets=3, $dateFormat="D jS M y H:i", $includeReplies=false, $dateTimeZone="Europe/London", $beforeTweetsHtml="<ul>", $tweetStartHtml="<li class=\"tweet\"><span class=\"tweet-status\">", $tweetMiddleHtml="</span><br/><span class=\"tweet-details\">", $tweetEndHtml="</span></li>", $afterTweetsHtml="</ul>") { date_default_timezone_set($dateTimeZone); if ( $twitter_xml = twitter_status($twitter_id) ) { $result = $beforeTweetsHtml; foreach ($twitter_xml->status as $key => $status) { if ($includeReplies == true | substr_count($status->text,"@") == 0 | strpos($status->text,"@") != 0) { $message = processLinks($status->text); ++$i; if ($i == $nooftweets) break; } } $result.=$afterTweetsHtml; } else { $result.= $beforeTweetsHtml."<li id='tweet'>Twitter seems to be unavailable at the moment</li>".$afterTweetsHtml; } echo $result; } ?>
Comments
Subscribe to comments
You need to login to post a comment.

Hello, for some reason, whenever I call get_tweets($user) I get the list but only with the dates and no tweets themselves. The code is unchanged, I'm just calling the funciont with a var which is a valid twitter name (like $user = "bsides";). Please help! :)
Ok I got it working. The error in your script is at line 55. The var $message doesn't have anything. To fix this, just replace line 55 with:
$message = processLinks($status->text);
Now I'm working around foreign signs and encoding issues, but overall, great script. Thanks a lot :)
Only just notices these comments, my bad, your completely correct and its now fixed. cheers.
hello .i hope you are doing well sir.sir i have a problem .i have you used your code.for the first time is was runnig.tweets were showing that time .but not got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is the possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan
hello .i hope you are doing well sir.sir i have a problem .i have you used your code.for the first time is was runnig.tweets were showing that time .but not got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is the possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan
hello .i hope you are doing well .sir i have a problem .i have used your code.for the first time is was runnig.tweets were showing that time .but now i got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is there any possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan
Thanks for this. Super stuff. I borrowed your hashtag/url parsing for my own Twitter RSS parser: http://f6design.com/journal/2010/10/07/display-recent-twitter-tweets-using-php/ (and gave you credit of course).
One thing to note: Twitter limits the number of times a user client can fetch their RSS feed per hour, so on a busy website this limit will be quickly reached. The way around this is to cache the tweets for one hour after they are fetched.