Papermashup

Subscribe


Tweets


"RT @Eva_Shaughnessy: #finedining @romomobilecafes restaurant this evening with #NewHair #winning #SaturdayNight #GirlsNightOut http://t.co/…"

@ashleyford 3 weeks ago

"RT @kycutwilson: @ashleyford @burgerbeartom incredible. There's 5 more left! Shout about it!!"

@ashleyford 1 month ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Using the Twitter Search API

AshleyAshley

Twitters new search feature is great for finding interesting people and topics to follow. So i wrote a script to get search results from twitter and display them on your site. This could be integrated into an existing application or used standalone to follow a particular topic or keyword on twitter,  Check the Demo.

twitter-search

The script uses PHP and CURL to get the twitter search results to display them on the page. for the demo i’ve set an if statement to display results for ‘papermashup.com’ if no get variable is present.

Here’s the code that allows us to enter our search term, on submit it posts a get variable to the URL which PHP then processes with CURL.


<div id="search">
<form action="" method="get">
  <label>
  Search twitter 
  <input type="text" name="q" id="searchbox" />
  <input type="submit" name="submit" id="submit" value="Search" />
  </label>
</form>
</div>

So we use PHP, CURL, and the SimpleXMLElement() class in PHP5 to parse the XML file. Once we have the xml data, regular expression is used to find the links in the xml content element, which is then saved in $description.

The Code:



// Date function (this could be included in a seperate script to keep it clean)
function date_diff($d1, $d2){
	$d1 = (is_string($d1) ? strtotime($d1) : $d1);
	$d2 = (is_string($d2) ? strtotime($d2) : $d2);

	$diff_secs = abs($d1 - $d2);
	$base_year = min(date("Y", $d1), date("Y", $d2));

	$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
	$diffArray = array(
		"years" => date("Y", $diff) - $base_year,
		"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
		"months" => date("n", $diff) - 1,
		"days_total" => floor($diff_secs / (3600 * 24)),
		"days" => date("j", $diff) - 1,
		"hours_total" => floor($diff_secs / 3600),
		"hours" => date("G", $diff),
		"minutes_total" => floor($diff_secs / 60),
		"minutes" => (int) date("i", $diff),
		"seconds_total" => $diff_secs,
		"seconds" => (int) date("s", $diff)
	);
	if($diffArray['days'] > 0){
		if($diffArray['days'] == 1){
			$days = '1 day';
		}else{
			$days = $diffArray['days'] . ' days';
		}
		return $days . ' and ' . $diffArray['hours'] . ' hours ago';
	}else if($diffArray['hours'] > 0){
		if($diffArray['hours'] == 1){
			$hours = '1 hour';
		}else{
			$hours = $diffArray['hours'] . ' hours';
		}
		return $hours . ' and ' . $diffArray['minutes'] . ' minutes ago';
	}else if($diffArray['minutes'] > 0){
		if($diffArray['minutes'] == 1){
			$minutes = '1 minute';
		}else{
			$minutes = $diffArray['minutes'] . ' minutes';
		}
		return $minutes . ' and ' . $diffArray['seconds'] . ' seconds ago';
	}else{
		return 'Less than a minute ago';
	}
}





// Work out the Date plus 8 hours
// get the current timestamp into an array
$timestamp = time();
$date_time_array = getdate($timestamp);

$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];

// use mktime to recreate the unix timestamp
// adding 19 hours to $hours
$timestamp = mktime($hours + 0,$minutes,$seconds,$month,$day,$year);
$theDate = strftime('%Y-%m-%d %H:%M:%S',$timestamp);	



// END DATE FUNCTION




//Search API Script

$q=$_GET['q'];

if($_GET['q']==''){

$q = 'papermashup.com';}

$search = "http://search.twitter.com/search.atom?q=".$q."";

$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>Twitter search results for '".$q."'</h3>";

## Echo the Search Data

foreach ($search_res->entry as $twit1) {

$description = $twit1->content;

$description = preg_replace("#(^|[n ])@([^ "tnr<]*)#ise", "'\1<a href="http://www.twitter.com/\2" >@\2</a>'", $description);  
$description = preg_replace("#(^|[n ])([w]+?://[w]+[^ "nrt<]*)#ise", "'\1<a href="\2" >\2</a>'", $description);
$description = preg_replace("#(^|[n ])((www|ftp).[^ "tnr<]*)#ise", "'\1<a href="http://\2" >\2</a>'", $description);

$retweet = strip_tags($description);


$date =  strtotime($twit1->updated);
$dayMonth = date('d M', $date);
$year = date('y', $date);
$message = $row['content'];
$datediff = date_diff($theDate, $date);



echo "<div class='user'><a href="",$twit1->author->uri,"" target="_blank"><img border="0" width="48" class="twitter_thumb" 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'>Retweet!</a></div><strong>".$datediff."</strong></div><div class='clear'></div></div>";

}


curl_close($tw);



UPDATE 25/03/09: i’ve added a re-tweet button which simply opens twitter putting the update in your status window.

UPDATE 26/04/09: As requested by @vincent below. I’ve added a time function which works out how long ago a tweet was posted

demodownload

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 70
  • fitra
    Posted on

    fitra fitra

    Reply Author

    Hi Asley,
    i have problem below

    Warning: SimpleXMLElement::__construct() [simplexmlelement.–construct]: Entity: line 1: parser error : Start tag expected, ‘__construct(‘$twi’) #1 {main} thrown in twitter-search.php on line 105

    can you help me please, thanks


  • Mark
    Posted on

    Mark Mark

    Reply Author

    Thanks for a great script


  • Mert
    Posted on

    Mert Mert

    Reply Author

    Fatal error: Cannot redeclare date_diff() in C:xampphtdocstwittatwitter-search.php on line 106

    Solution:
    xampp ==> remove from your PC.
    AppServ ==> install it.

    This problem is solved. 😉

    Thanks Ashley… Great article…


  • stefano
    Posted on

    stefano stefano

    Reply Author

    Can you update it so that we can search for complete phrases ? right now, it’s a mix of results.

    ie. “game of thrones”

    Thanks!!


  • search tweet
    Posted on

    search tweet search tweet

    Reply Author

    Hello…Ashley..Can you add example code for how many tweet display on 1st page and add next page etc for this?

    I mean I want to set the search results only show 40 tweet results and 20 for the 1st page and 20 fot the second page. Can you do it? Plsss


  • m
    Posted on

    m m

    Reply Author

    @Tino: you get that error because the php version u r currently using is > 5.3.0 and there is already a function called date_diff(), so to avoid that error you simply have to rename the function in the file. It worked for me.


  • Pretty Vincent
    Posted on

    Pretty Vincent Pretty Vincent

    Reply Author

    Thanks for the script, I’m gonna try it


  • Tino
    Posted on

    Tino Tino

    Reply Author

    Thanks for this great tutorial Asley.

    I tried the code in my localhost and get an error like Carlos was get.

    Fatal error: Cannot redeclare date_diff() in C:xampphtdocstwittatwitter-search.php on line 106

    I hope you have a time to solve the problem.

    Thanks


  • Nishant
    Posted on

    Nishant Nishant

    Reply Author

    Hi Ashley,

    I got the solution, I needed to restart my Apache and now cULR is working.
    Thanks


  • Nishant
    Posted on

    Nishant Nishant

    Reply Author

    Hi Ashley,

    Thanks for sharing this nice tutorial. I am running XAMPP on Windows XP and when I am writing the above script, I am getting the following error:

    “Fatal error: Call to undefined function curl_init() in C:xampphtdocsAutomation ToolcURL.php on line 2″

    I searched on internet and found that I have to do two things in order for cURL to work for Windows:
    1. Uncomment extension=php_curl.dll in the PHP.INI file.
    2. Copy the below two dll files from PHP folder and put it under windows/system32 dir.
    ssleay32.dll
    libeay32.dll

    I have done it but still its not working. Please help me.


  • Nishant
    Posted on

    Nishant Nishant

    Reply Author

    Hi Ashley,

    Thanks for sharing this nice tutorial. I am running XAMPP on Windows XP and when I am writing the above script, I am getting the following error:

    “Fatal error: Call to undefined function curl_init() in C:xampphtdocsAutomation ToolcURL.php on line 2″

    I searched on internet and found that I have to do two things in order for cURL to work for Windows:
    1. Uncomment extension=php_curl.dll in the PHP.INI file.
    2. Copy the below two dll files from PHP folder and put it under windows/system32 dir.
    ssleay32.dll
    libeay32.dll

    I have done it but still its not working. Please help.


  • babar
    Posted on

    babar babar

    Reply Author

    Grate tutorial …..
    Can u kindly help me, I am trying to get a count of total tweeks on twitter that contain a specific search word. What I tried is after parse the atom feed using simplexml, I use the ‘count’ method to get the number of “entry” items. But it gives probably what is returned in one feed while I want to get count of all tweets on twitter.


  • Ramesh
    Posted on

    Ramesh Ramesh

    Reply Author

    Could you please help for search result pagination


  • Ramesh
    Posted on

    Ramesh Ramesh

    Reply Author

    In that search result page, I am getting very limited results like(15) only,how can i get all the related result.


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @ Ramesh,

      I think the search results are limited, although im not sure by how many you could probably get several hundred results try &count=500 at the end of the CURL XML request.

      Ashley


  • saief
    Posted on

    saief saief

    Reply Author

    Hi. Do you think you can add real time search feature, Twitter Trends, your search, Key word search… i would like to know how its works.


  • saief
    Posted on

    saief saief

    Reply Author

    Can I use this search script to create my own twitter search engine. if so how can i do that. i am not tech person just wanted to know for my class project. so can please help me


  • Phil
    Posted on

    Phil Phil

    Reply Author

    @Maria @carlos I had the same problem as you. I don’t know why I hit the issue but if you just rename the date_diff function to something else and then change the name where the function is called lower down the page then it should work.


  • Aejaz
    Posted on

    Aejaz Aejaz

    Reply Author

    Hello Ashley,

    Thanks for the Nice tutorial.

    I need a help in pagination.

    my query is how can i retrieve all the tweets max it is 1500 given limit in the api.
    I want to show all the tweets in my php page with pagination.1234…. or previous | next

    how can i do that pls help me out.

    I am able to receive 100 tweets for one page.
    how can i paginate the remaining tweets the rest of 1400 tweets of other page.

    like we have rpp=20&page=1 how can i increment this page name so that i can navigate all the 1500 tweets.

    Thanks,
    Aejaz


  • Maria
    Posted on

    Maria Maria

    Reply Author

    Please, I have the same error:

    Fatal error: Cannot redeclare date_diff() in C:xampphtdocstwittatwitter-search.php on line 106

    Somebody can help me?


  • carlos
    Posted on

    carlos carlos

    Reply Author

    Awesome! its exactly what i was looking for!

    Something is wrong with those files i downloaded, at least with mine. Its giving an error.

    Fatal error: Cannot redeclare date_diff() in C:xampphtdocstwittatwitter-search.php on line 106

    Well in your style sheet would be line 47 i guess!

    Can you help me with that! It’s an emergency!

    Thanks


  • Rees
    Posted on

    Rees Rees

    Reply Author

    awesome tutorial ashley, thanks so much for that! I was curious, is there a quick and efficient way to make the search results appear “live” or automatically refresh whenever a new twit comes in?

    my first thought is to simply have a javascript function execute an ajax call to this PHP in your tutorial every 3-5 seconds…but is that the best way to get that “live” effect? if not, what you would suggest??

    Thanks in advance!


  • Laurier
    Posted on

    Laurier Laurier

    Reply Author

    Hi, I’m trying to do something rather simple. I need the search to check for exact matches, as if they had the ” double quotes. How would I do this?


  • Jose Augusto
    Posted on

    Jose Augusto Jose Augusto

    Reply Author

    Does not work with two words queries like “domain name”.
    You better url encode the query ($q)


  • e-sushi
    Posted on

    e-sushi e-sushi

    Reply Author

    Tnx for sharing… a great resource to start from (if you’re lazy like me – lol)


  • Matt
    Posted on

    Matt Matt

    Reply Author

    Hey Ashley!
    Thanks for this great great tutorial and demo, I’m right now playing with it.
    I’m stuck trying to add a jquery auto refresh to add the new tweets every x seconds, can you help me?
    Best regardst


  • Marcel
    Posted on

    Marcel Marcel

    Reply Author

    Hi Ashley,

    Would you be willing to give me some tips on how to change this code in order to search for a term in the tweets of a group of users in a particular Twitter list?

    Many thanks in advance.

    Best regards,
    Marcel


  • jake
    Posted on

    jake jake

    Reply Author

    not working.

    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /home/penpal/public_html/index.php:151 Stack trace: #0 /home/penpal/public_html/index.php(151): SimpleXMLElement->__construct(”) #1 {main} thrown in /home/penpal/public_html/index.php on line 151


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @jake can you provide a little more information, what did you modify in the script?


  • designfollow
    Posted on

    designfollow designfollow

    Reply Author

    great.

    thank you.


  • raghu
    Posted on

    raghu raghu

    Reply Author

    Hi, thanq for your valuable script. Here the results has to open in new page. thats y u gave as blank

    “.$description.”From: “, $twit1->author->name,”

    plz rectify this…


  • lapz
    Posted on

    lapz lapz

    Reply Author

    Hi, thanks for this great script.
    I was wondering if there was a simple way to sort the posts from the oldest to the newest ?

    @Teh, sometimes Twitter search results only display recent tweets. It comes back afterwards.


  • Teh
    Posted on

    Teh Teh

    Reply Author

    Hello,

    Thanks for your script.
    I was testing it with this date “Tue Mar 21 21:00:54 +0000 2006″.
    It returns a wrong count. I guess you are not handling anything more than a year.


  • Rick
    Posted on

    Rick Rick

    Reply Author

    Hello, great script. How would I go about making the links in each individual tweet have a target of “blank” so that it would pop up in another window? Thanks.

    Rick


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @rick You could use jquery to add the attribute target with a value of _blank to each of the links.


  • dmkinteractive
    Posted on

    dmkinteractive dmkinteractive

    Reply Author

    Hi there, this script is brilliant! I just have one question, is there any way to get multiple searches and include them all in the same output ordered by time?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @dmkinteractive you would have to write a script to loop through the search terms and do a curl request. i’d also recommend caching popular search results to limit the amount of times you ping twitter. Hope that helps 😉


  • Dallas Web Design
    Posted on

    Dallas Web Design Dallas Web Design

    Reply Author

    Does nayone use the twitter badge on the site. Most people dont even know about it but its pretty cool . Just copy and paste tweek the css to the way you want it to look and all your tweets go right on your page. Cool to use for blog web design or something.


  • Miami Mike
    Posted on

    Miami Mike Miami Mike

    Reply Author

    Here is a similar script that uses server side caching and time delays so that Twitter won’t block your access due to overuse: http://spaceninja.com/2009/07/twitter-php-caching/


  • Yannik
    Posted on

    Yannik Yannik

    Reply Author

    Fatal error: Call to undefined function curl_init() in /mnt/web6/21/82/51616782/htdocs/twitter.php on line 146

    what is curl, and what to do, that it works?


  • prabal
    Posted on

    prabal prabal

    Reply Author

    Thanks for a great script… really nice script by using jQuery.
    i have work on number of web service API but this one is best for me.


  • Jeppe D
    Posted on

    Jeppe D Jeppe D

    Reply Author

    Hi,

    Nice example.

    But when I use it the date is wrong. A tweet from yesterday evening seems to be 5 days and 19 hours old.
    Are there any problems because of timezones?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @jeppe my server is in Los Angeles and i’m in London UK, so to compensate for the time difference there’s this line of code.

      $timestamp = mktime($hours + 0,$minutes,$seconds,$month,$day,$year);

      you need to change the 0 above to however many hours time difference there is between you and your server.

      Hope that helps,


  • saif
    Posted on

    saif saif

    Reply Author

    Dear
    nice tutorial.but help me…..how can i get the id of particular content?
    I have tried by ‘ $twit1->id’. result is not only id but with ‘tag:search.twitter.com……..’
    i need id to hyperlink with it to go to the specific update.

    hope u will understand my problem.
    is there any list, what ‘$search_res = new SimpleXMLElement($twi);’ return?……..means content,updated & what others?

    Awaiting for yr reply


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @saif firstly to see what data you can get from the xml file its best just to view it (in firefox not safari. Firefox removes xml style so you can see the xml nodes) if you just want to link to the specific status this line is in the xml file:
      you can simply target the attribute href by using the following line: link[0]->attributes()->href; ?> let me know if that makes sense.


  • Gregg
    Posted on

    Gregg Gregg

    Reply Author

    Thanks so much for this code!! The date function is really great. I’m a real novice here–any chance you could help me modify the date function above to work with the friends-timeline API rather than the search API? What would I change to make it work?


  • Perry
    Posted on

    Perry Perry

    Reply Author

    In addition to my last comment, I now see where your demo exhibits the same behaviour as does mine where no results from a search are displayed.

    Any comments?


  • Perry
    Posted on

    Perry Perry

    Reply Author

    Have you heard of an IP address being blocked by Twitter for using this script or a derivative of it?

    I had modified your script and had been using it successfully on a website, but now, it won’t provide results.

    How can I tell if I’m being blocked?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @perry no i can’t say i have with the search api…


  • Andrew
    Posted on

    Andrew Andrew

    Reply Author

    Hi,

    Thanks for a great script… any ideas how I can limit the amount of items that are displayed? Also a good idea is to include the username in the retweet after the RT: @ – I did manage to do this but it includes the users name in brackets which is not needed.

    Cheers,

    Andy


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @andrew you can limit the items by simply adding an if statement

      $num=0;
      if($num < 5){

      // wrap the if statement just inside the foreach statement

      $num++;
      }

      let me know if you have any problems


  • Rod
    Posted on

    Rod Rod

    Reply Author

    Hashes weren’t working – also spaces were yielding incorrect results. On twitter if you search two keywords it finds results with BOTH, with yours it would find results with EITHER. To fix both of these you need to redefine the URL encoding after pulling down the info with GET – eg;

    $q = str_replace(” “, “%20″, $q); // put the space code back
    $q = str_replace(“#”, “%23″, $q); // put the hash code back

    Should fix it up.


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @Rod you’ll need to urlencode() the search query thats passed to the xml file, it’s something i missed out in the example code. Hope that helps.


  • Rod
    Posted on

    Rod Rod

    Reply Author

    Looks great at first glance, but for some reason I can’t get the demo to work with hash tags… #anything returns no results… intentional? it’s a pretty crucial omission for me


  • Vincent
    Posted on

    Vincent Vincent

    Reply Author

    Appreciate your doing this,worthing learning. I try to integrate google translate API or other translate jQuery into this.so I can translate the search result to any language.


  • vincent
    Posted on

    vincent vincent

    Reply Author

    it cool,is it possibel to show the tweet time in the tweeet


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @vincent I’ve updated the demo and download to include the time as well as a function to work out the time since a tweet was posted.


  • nick
    Posted on

    nick nick

    Reply Author

    Isn’t it standard practice to include the original tweeter when retweeting?

    Also then there is a ‘ it cuts off the rest of the message.


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @nick yeah it is, i added the retweet as an example you are more than welcome to extend the script for your own use.


  • nick
    Posted on

    nick nick

    Reply Author

    Awesome. I have almost 0 php skills and this was the only thing missing.

    Thanks!


  • nick
    Posted on

    nick nick

    Reply Author

    Hey,

    Is it possible to add a retweet button to each of the results?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @nick I’ve updated the script and added a retweet button, you could extend this and mash it with my ‘using the twitter api’ post to make it automatic but as a simple example you click the re-tweet button and it adds the status in the twitter form.

      Hope that helps


  • Simon Lewis
    Posted on

    Simon Lewis Simon Lewis

    Reply Author

    Great tutorial this is exactly what i was looking for an app im building thanks Ashley


  • Ashley
    Posted on

    Ashley Ashley

    Reply Author

    Hey Simon,

    Glad you found it useful, thanks for dropping by!