Yes, there are java script widgets available which will display the feeds on your site, but quite often they don't gel with the design of your site. So, here is a small code which can get you the feeds in php and then you can format them for your site in the way you want them to.
http://search.twitter.com/search.json?q=from:XXXXX
This is the magic URL which gives us the required data and the search.json tells Twitter that we want the data in JSON format. The XXXXX can be replaced with the keyword we want data on. For example, I will change the URL to http://search.twitter.com/search.json?q=from:justread7 which will mean I will get the tweets by the user justread7.
So, here is the PHP code through which we will get the JSON data decoded.
$data = 'http://search.twitter.com/search.json?q=from:justread7';
$feed = file_get_contents($data); //Getting the JSON data.
$valid_data = json_decode($feed); // Converting the JSON data to PHP format.
$valid_data = $valid_data->results; // Valid data now with just the tweet result.
// Printing out the feed's data in our required format.
print '<pre>';
foreach ($valid_data as $key=>$value) {
print '<div id="twitter-data-container">';
print '<img src="' . $value->profile_image_url . '" />';
print '<p>' . $value->text . '</p>';
print '</div>';
}
print '</pre>';
First we get $feed which is using the PHP function file_get_contents to retrive the JSON data. Then I using json_decode we convert the JSON data into PHP format and once, that is done it's just a matter of reading the data and building the output.
Here is a screen shot of the modified PHP array in a crude form. Obviously I have not worked on the CSS to decorate. But very soon I will extend this tutorial to create a custom block in Drupal 7 to show my tweet results as a part of my site's design.
Printing the PHP array of Twitter feeds
One thing which I forgot to mention in my first piece of code and when I was working on it, I then realized that the twitter feeds don't have a link. And this is something which we would definitely like to have. So, I looked for some code and got a small piece of PHP code which does the job. I was not very keen on javascript code and hence the server side code.
So, here is a small change in the code above:
print '<p>' . $value->text . '</p>';
The line which is printing the text will passing the text through a function first
print '<p>' . amitavroy_site_block_linkify_twitter_status($value->text) . '</p>';
And here is the linkify code:
function amitavroy_site_block_linkify_twitter_status($status_text) {
// linkify URLs
$status_text = preg_replace(
'/(https?:\/\/\S+)/',
'<a href="\1" class="preg-links">\1</a>',
$status_text
);
// linkify twitter users
$status_text = preg_replace(
'/(^|\s)@(\w+)/',
'\1@<a href="http://twitter.com/\2" class="preg-links">\2</a>',
$status_text
);
// linkify tags
$status_text = preg_replace(
'/(^|\s)#(\w+)/',
'\1#<a href="http://search.twitter.com/search?q=%23\2" class="preg-links">\2</a>',
$status_text
);
return $status_text;
}
Twitter feeds final output
One thing that I would like to mention in this article is that although we are able to read the feeds through PHP and JSON and this gives us a lot of power, but there is a flip side to it. If you have devel module installed, then check the page generation time. You will find that the page generation time increase by a huge amount because of the file_get_content being used. Yes, it is a powerful tool but when the content is coming from an outside domain, generally the time to read the file increase a lot and that can slow down the process and hence you need a very strong mechanism to cache it, or simply use the widget. (As said a lot of times, good things comes as a cost).