Tuesday, July 28, 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="http://www.twitter.com/'.$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

...so, 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!

Related posts

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

Bookmark and Share
Digg this

No comments: