Twitter Search API and PHP Tutorial to get real-time index of recent Tweets!

NOTE:
THIS CODE NO LONGER WORKS. TWITTER API V1 RETIRED.
READ HERE https://dev.twitter.com/blog/api-v1-is-retired

How can we use the Search Twitter API to search against the real-time index of recent Tweets?. It’s not so difficult if you know PHP CURL function and XML little bit.

NOTE:

This script uses Curl Function to communicate and execute the external server file. Please make sure Curl and SimpleXML is enable or not in your Server.

First you have to know how many formats of API is there for Twitter Search, below is the list of formats where we can use it for.

i.   ATOM
ii.  XML
iii.  JSON

In this tutorial I’m going to teach you about how to use atom feed, let me show the URL First,

 http://search.twitter.com/search.atom?q=java

If you run the above URL in a browser it will show the RSS Feed. Here I’m going to show how to fetch the value from the RSS Feed.


<?php

// get the keyword from url
$srchterm = $_GET['q'];

// encode it for the safe search
$srchterm = urlencode($srchterm);

// search the keyword using Twitter API
$srch_twitts = "http://search.twitter.com/search.atom?q=".$srchterm."";

// use curl to execute the Twitter URL
$twits = curl_init();
curl_setopt($twits, CURLOPT_URL, $srch_twitts);
curl_setopt($twits, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($twits);

// here we have the searched result in an array
$search_res = new SimpleXMLElement($twi);

//print_r($search_res);

?>

See the above code it is well commented, line no.4 gets the value from the url and line no.7 encode the string for Safe Search. Line no.6 execute the Twitter API. Now line.19 has the array of value.
if you print the array of value you can see the tags like <entry><name><link><content> etc.,


<?php

/* display the data */

$i = 0;

// displays the search keyword
echo "<p style='padding-left:10px; color:brown'>Your search term is: " . stripslashes($_GET['q']) . "</p>";

// tweets in an array. split it by foreach
// we need only 10 result so, use if condition
foreach ($search_res->entry as $result) if ($i++ < 10)
{

echo "<div id='tweets'>";
echo "<p>$i</p> ";
echo "<div class='avatar'><img src='". $result->link[1]->attributes()->href. "' /> </div>";
echo "<div class='twitcont'>";
echo "<div class='name'>". $result->author->name . "</div>";
echo "<div class='content'>" . $result->content ;

// convert the updated date of tweet to seconds
$twittedSeconds = time() - strtotime($result->updated);
$names = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
$seconds = array( 1,        60,       3600,   86400, 604800, 2630880, 31570560);

// find the time difference between present time and tweet time

if($twittedSeconds > 0)
{
for($j = count($seconds) - 1; $j >= 0 ; $j--)
{
$interval = $seconds[$j];
if($twittedSeconds >= $interval)
{
$getTime =  round($twittedSeconds / $interval);
if ($getTime > 1)
{
$names[$j] .= s;
}
$time = $getTime. ' ' . $names[$j] . ' ago';
break;
}
}
//echo the time difference
echo "<div class='time'> " . $time . "</div>";
}
echo "</div>";
echo "</div></div>";

}

?>

Use the above PHP code to echo all the value from the Array using “foreach” statement. For easy of use, I have given all the codes in one shot without stylesheet. Download otherwise if you want with the style sheet. Enjoy!


<?php

// get the keyword from url
$srchterm = $_GET['q'];

// encode it for the safe search
$srchterm = urlencode($srchterm);

// search the keyword using Twitter API
$srch_twitts = "http://search.twitter.com/search.atom?q=".$srchterm."";

// use curl to execute the Twitter URL
$twits = curl_init();
curl_setopt($twits, CURLOPT_URL, $srch_twitts);
curl_setopt($twits, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($twits);

// here we have the searched result in an array
$search_res = new SimpleXMLElement($twi);

//print_r($search_res);

?>

<!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>My Twitter</title>
</head>
<body>
<div id="top">
<div>
<form action="" method="get" >
Search Tweets: <input type="text" name="q" />
</form>
</div>
<div>
<div id="wrapper">
<div id="container">

<?php

/* display the data */

$i = 0;

// displays the search keyword
echo "<p style='padding-left:10px; color:brown'>Your search term is: " . stripslashes($_GET['q']) . "</p>";

// tweets in an array. split it by foreach
// we need only 10 result so, use if condition
foreach ($search_res->entry as $result) if ($i++ < 10)
{

echo "<div id='tweets'>";
echo "<p>$i</p> ";
echo "<div class='avatar'><img src='". $result->link[1]->attributes()->href. "' /> </div>";
echo "<div class='twitcont'>";
echo "<div class='name'>". $result->author->name . "</div>";
echo "<div class='content'>" . $result->content ;

// convert the updated date of tweet to seconds
$twittedSeconds = time() - strtotime($result->updated);
$names = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
$seconds = array( 1,        60,       3600,   86400, 604800, 2630880, 31570560);

// find the time difference between present time and tweet time

if($twittedSeconds > 0)
{
for($j = count($seconds) - 1; $j >= 0 ; $j--)
{
$interval = $seconds[$j];
if($twittedSeconds >= $interval)
{
$getTime =  round($twittedSeconds / $interval);
if ($getTime > 1)
{
$names[$j] .= s;
}
$time = $getTime. ' ' . $names[$j] . ' ago';
break;
}
}
//echo the time difference
echo "<div class='time'> " . $time . "</div>";
}
echo "</div>";
echo "</div></div>";

}

?>
</div>
</div>
</body>
</html>

Copy the above code and use it or download the script with the stylesheet if you wish!

DemoDownload

4 Comments

Add a Comment

Your email address will not be published. Required fields are marked *