Since Twitter came to existence there have been many attempts to clone the micro-blogging giant but twitter seems to be unmatchable! The main reason of its success is its API which lets a developer do anything. Commonly PHP/MYSQL is used to use the API offered by twitter. With the Twitter API you can update your Twitter statuses remotely, search the latest tweets for a keyword, grab someone’s or your latest Tweets and many other innumerable things. But today, you’ll learn to implement the simple search API to search the tweets for a specific keyword remotely (from another website or host) using PHP. So lets get started!
The package contains the following files:
The “index.php” file contains the PHP code used for searching Twitter for the Tag entered while the “search.php” file contains the search form used in the tutorial. The two files can be joined if you want. You just have to delete the following line:
require_once('search.php');
with the following thing:
<div id="search"> <form action="" method="get"> <label style="color:#FFFFFF"> <strong>Search twitter : </strong> <input type="text" name="query" id="searchbox" /> <input type="submit" name="submit" id="submit" value="Search" /> </label> </form> </div>
and then delete the “search.php” file as it would be of no use then. The main function of the “require_once” function is to include a file while executing the script.
The “index.php” file contains the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Tutorialswalk.info | Twitter Search API | PHP Tutorial</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header"><a href="http://tutorialswalk.info"><center><img src="logo.png" width="300" height="80" border="0" /></center></a></div>
<div id="container" style="width:550px;margin-left:auto;margin-right:auto;">
<?php
require_once('search.php');
//Search API Script
$searchtag=$_GET['query'];
if($_GET['query']==''){
$searchtag = 'Twitter Search API';}
$search = "http://search.twitter.com/search.atom?q=".$searchtag."";
// CURL implementation
$tw = curl_init();
curl_setopt($tw, CURLOPT_URL, $search);
curl_setopt($tw, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($tw);
$search_res = new SimpleXMLElement($twi);
echo "<h3>Search results for '".$searchtag."'</h3>";
echo "<div style='border-bottom:#000000 thin dotted'></div>";
## Echo the Search Data
foreach ($search_res->entry as $twit1) {
$description = $twit1->content;
$description = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $description);
$retweet = strip_tags($description);
$message = $row['content'];
echo "<div class='user'><a href=\"",$twit1->author->uri,"\" target=\"_blank\"><img border=\"0\" width=\"48\" class=\"twitter\" src=\"",$twit1->link[1]->attributes()->href,"\" title=\"", $twit1->author->name, "\" /></a>\n";
echo "<div class='text'>".$description."<div class='description'>From: ", $twit1->author->name," <a href='http://twitter.com/home?status=RT: ".$retweet."' target='_blank'><img src='retweet.png' style='border:none;' /></a></div><strong>".$datediff."</strong></div><div class='clear'></div></div>";
}
curl_close($tw);
?>
</div>
<div id="footer"><center><a href="http://www.tutorialswalk.info">Tutorialswalk</a> | <a href="http://tutorialswalk.info">Back to tutorial</a></center></div>
</body>
</html>
The PHP code above takes the search-tag you enter and then it passes it on the Twitter API in line 28:
$search = "http://search.twitter.com/search.atom?q=".$searchtag."";
which returns the tweet(s) containing the search-tag with a description of the user who entered the tweet.
The “search.php” file just has a form with an input box and a submit button:
<div id="search"> <form action="" method="get"> <label style="color:#FFFFFF"> <strong>Search twitter : </strong> <input type="text" name="query" id="searchbox" /> <input type="submit" name="submit" id="submit" value="Search" /> </label> </form> </div>
The “style.css” file contains the following code/styling:
body{
font-family:Verdana, Geneva, sans-serif;
font-size:14px;}
.twitter{
float:left;
margin-right:20px;
margin-bottom:0px;
}
.user{
margin-bottom:10px;
border-bottom:#CCCCCC thin dotted;
padding:10px;}
#container {
border-style:solid;
border-width:1px;
border-color:#3f3d3d
}
.clear{
clear:both;
}
#search{
padding:8px;
background-color:#85298d;
}
The other two files in the package are the image files and just contain a Retweet button and a logo which I made using Photoshop CS4. Their are a number of things you can do with this idea:
- Using the PHP date & time functions grab the Date and Time of the tweet and then convert that time to the server time.
- Add more functions and styles to the script.
- Embed the code onto your next project.
As this is my first PHP coding tutorial, I expect some errors in the code as well as my explanation of the code. If you find any errors or have a suggestion then please feel free to leave a comment and I’ll get back to you!


Posted in 

Hi,
I trying to do something for a project. I’m trying to take a twitter search and output just the tweets, no other data (avatar, time etc) to html. So just the characters within the tweet are output not in any shell like twitters widget. I’m struggling to find code anywhere to do this.
Any help you can give me would be great.
Willy