Last week I wrote about how to quickly embed a Tweet into your web page using only a couple of lines of PHP. This technique had a few shortcomings – it wasn’t the tidiest way of embedding multiple Tweets and it didn’t format the Tweet with any entities (links, @mentions and #tags were displayed as plain text).
This update shows you how to load your Tweets as a JSON string, convert that string into a PHP object and then format your Tweets as HTML using a simple function. This example will produce an un-ordered list of between 1 and 19 of your most recent Tweets.
The Function:
<?php
function htmlTweet($username,$limit=1){
// API limits number of Tweets returned to 20
// Sanity check to prevent errors from happening due to invalid inputs
if($limit>19)$limit=19;
if($limit<1)$limit=1;
// Reset Loop Counter
$i=0;
// Opening HTML Tags - Function generates a UL
$tweets='<ul class="tweet-list">'."n";
// Load your most recent Tweets from Twitter API, in JSON format
// This is a public function - no authentication required
// If your profile is set to private, this call will not work
$t=file_get_contents('https://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$username.'&trim_user=1&include_entities=1');
// Turn the JSON string into a PHP object
// JSON Decode must be installed - available since PHP 5.2.0
$t=json_decode($t);
while($i<$limit){
// Build arrays to store search and replacement values
// $search stores the original plain text @mention, #tag or URL
// $replace stores the generated HTML replacement
$search=array();
$replace=array();
// Build HTML replacements for @mentions
foreach($t[$i]->entities->user_mentions as $m){
$search[]='@'.$m->screen_name;
$replace[]='<a title="'.$m->name.' on Twitter (@'.$m->screen_name.')" target="_blank" href="http://twitter.com/'.$m->screen_name.'">@'.$m->screen_name.'</a>';
}
// Build HTML replacements for #tags
foreach($t[$i]->entities->hashtags as $h){
$search[]='#'.$h->text;
$replace[]='<a title="#'.$h->text.' on Twitter" target="_blank" href="http://twitter.com/search?q=%23'.$h->text.'">#'.$h->text.'</a>';
}
// Build HTML replacements for URL's
foreach($t[$i]->entities->urls as $u){
$search[]=$u->url;
$replace[]='<a title="'.$u->expanded_url.'" target="_blank" href="'.$u->url.'">'.$u->display_url.'</a>';
}
// Build HTML Tweet using str_replace and $search and $replace arrays
// Append this to previously generated Tweets then increment loop counter
$tweets.="t<li>".str_replace($search,$replace,$t[$i]->text)."</li>n";
$i++;
}
return $tweets."</ul>";
}
?>Using the Function:
By default this function will create an un-ordered list containing a single Tweet from the user specified in the ‘username’ parameter (please note – do not include an @), for example:
<?php echo htmlTweet('jedatron'); ?>Will output:
<ul class="tweet-list"> <li>My formatted tweet <a title="http://www.edcoleridgesmith.co.uk/" href="http://bit.li/edsblog" target="_blank">bit.ly/edsblog</a></li> </ul>
Or as your visitors will see it:
- My formatted tweet bit.ly/edsblog
You can display up to 19 Tweets in an un-ordered list using this function by specifying the number you want using the optional ‘limit’ parameter, like so:
<?php echo htmlTweet('jedatron',3); ?>Will output:
<ul class="tweet-list"> <li>My formatted tweet <a title="http://www.edcoleridgesmith.co.uk/" href="http://bit.li/edsblog" target="_blank">bit.ly/edsblog</a></li> <li>This Tweet has a <a title="#hash_tag" href="https://twitter.com/search?q=%23hash_tag" target="_blank">#hash_tag</a></li> <li>This Tweet mentions <a title="Ed Coleridge Smith on Twitter (jedatron)" href="http://twitter.com/jedatron" target="_blank">@jedatron</a></li> </ul>
Or as your visitors will see it:
- My formatted tweet bit.ly/edsblog
- This Tweet has a #hash_tag
- This Tweet mentions @jedatron
The function performs basic sanity checks to prevent errors from happening if an invalid parameter is used. For example, if you requested the function to display a number of Tweets greater that 19, only 19 Tweets would actually be displayed.