/ 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
Copy this code and paste it in your HTML
<?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) { $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>") { 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; } ?>