This blog has moved here: | FOLLOW ME ON TWITTER @woork
Saturday, June 27, 2009

Simple PHP Twitter Search ready to use in your web projects

After my previous tutorial that illustrated how to implement a super simple way to work with Twitter API (PHP + CSS) I received a lot of emails from my readers that asked to me to write a post about how to implement a simple Twitter search and display search results in a web page with a custom format. So in this tutorial I want to illustrate how you can implement that using a very useful Twitter Search API for PHP developed by David Billingham.

Using this simple method you can obtain, just in some lines of PHP and CSS code, awesome results like this:

You can download the source code at the following link and reuse it for free in your web projects just in some seconds!

1. A little introduction
In this package you'll find two PHP file:

- index.php: the search page (search form + search results)
- search.php: Twitter Search API for PHP

You have to customize only index.php and your Twitter Search will be ready to be integrated into yor web projects in one minute!

2. Index.php
Take a look at index.php. This page contains a simple search form:

<form action="index.php" method="submit">
<input name="twitterq" type="text" id="twitterq" />
<input name="Search" type="submit" value="Search" />

...and some lines of PHP code:

$twitter_query = $_GET['twitterq'];
$search = new TwitterSearch($twitter_query);
$results = $search->results();

foreach($results as $result){
echo '<div class="twitter_status">';
echo '<img src="'.$result->profile_image_url.'" class="twitter_image">';
$text_n = toLink($result->text);
echo $text_n;
echo '<div class="twitter_small">';
echo '<strong>From:</strong> <a href="'.$result->from_user.'">'.$result->from_user.'</a>: ';
echo '<strong>at:</strong> '.$result->created_at;
echo '</div>';
echo '</div>';

$result is the array that contains search results. To display all elements of the array (search results) I used this simple loop:

foreach($results as $result){

... and to get a specific attribute of the array I used this simple code:


The structure of a tweet contains the following attributes (take a look at the Twitter API home page for a full list):

[text]: Text of the current tweet
[to_user_id]: User Id
[from_user]: User name
[id]: Tweet Id
[from_user_id]: User id
[source]: Source of the current tweet
[profile_image_url]: User profile image URL
[created_at]: Date of the current tweet, if you want to display the text of a tweet you can use this code:


The following line of code get the text of the current tweet and convert a textual link into a clickable link:

$text_n = toLink($result->text);

That's all! Now download the source code, open and upload all files in your test server, open index.php and try to search for something!
If you have some suggestion, please add a comment!

Super simple way to work with Twitter API (PHP + CSS)Send messages from a PHP page using Twitter API

blog comments powered by Disqus
Simon Bennett said...

This is helpfull for my project looks really light weight .


Dave said...

YOU ARE THE BEST!!!!! Thanks a lot!

Mikael said...

you've done a great post. you should open a school

Hirvesh said...

Thanks for the nice tut. You really rock Antonio!

twitter backgrounds said...

Very cool! Going to take a look at this code and try to implement it on our site. Will be a great addition to the other Twitter things we have on our site.

Ryan Faerman said...

I'd just like to point out that while the PHP implementation of the search API is based on David Billingham's work, I actually developed it.

You can see the full documentation here:

twitter-api-announce said...

nice!I'm just looking for this

Adam said...

very very good.. however can i see more results.. what do i need to insert to view more results. page 1 2 3 4 5 etc???

adam said...

how can i open external links in new window??

and please reply back about viewing more pages

Kelli Piperata said...


  • Twitter Follow woork on Twitter
  • RSS Feed Subscribe to stay up to date
  • Podcast Coming soon...
  • 0 delicious lovers save
Share your links. Do you want to suggest any interesting link about web design or tech news? Submit your link.
Submit a News