Search Twitter using PHP
anoopsachari | Apr 20, 2010 | Comments 2
This tutorial would help you to implement the simple search API provided by twitter and can be used in any web-projects . Its fast, effective and very simple to use . I have put together the code in a neat format for ease of use .
The package contains 4 files :
1. index.php – contains the main functions to search twitter timeline
2. include_form.php – a form to search new tags
3. style.css – contains the css needed
4. an image file for retweet
source in index.php
<!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>Achari.in | Twitter Search | PHP </title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header"><a href="http://www.achari.in/"><img src="" width="348" height="63" border="0" /></a></div>
<div id="container" style="width:550px;margin-left:auto;margin-right:auto;">
<?php
require_once('include_form.php');
//Search API Script
$searchtag=$_GET['query'];
if($_GET['query']==''){
$searchtag = 'php';}
$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);
?>
<a href="http://www.achari.in">achari.in</a> | <a href="">Back to tutorial</a>
</div>
<div id="footer"></div>
</body>
</html>
style sheet content : style.css
.twitter{
float:left;
margin-right:20px;
margin-bottom:0px;
}
body{
font-family:Verdana, Geneva, sans-serif;
font-size:14px;}
.user{
margin-bottom:10px;
border-bottom:#CCCCCC thin dotted;
padding:10px;}
.clear{
clear:both;
}
#search{
padding:8px;
background-color:#5599BB;
}
source in include_form.php
<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>
You have done !
The API call passed to twitter is
$search = "http://search.twitter.com/search.atom?q=".$searchtag."";
where the $searchtag contains the tag you search for under twitter timeline .
Comment me if you have any qns .
Filed Under: php
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.







what is the use of it???
Hi Anoop.
Can you please tell me what’s happening in the two preg_replace functions. What are you trying to do der?