Twitter feed authentication (API 1.1) for search

27 April, 2013 by Tom Elliott

This tutorial builds upon my previous two guides for Authenticating a twitter feed using OAuth and PHP (for any public Twitter user timeline) and creating a customised Twitter feed using HTML, jQuery and CSS.

Once you have the Twitter authentication working using OAuth and PHP, altering the PHP query to create a custom Twitter search is pretty straightforward. You can search for any key word, hashtag or Twitter handle you like, or use a combination of these searches terms.

Here’s a demo of a custom twitter search. It is based on mentions/tweets of @timberners_lee (founder of the Internet), the search term ‘netneutrality’ and the hashtag ‘openinternet’.

The Twitter Search PHP:

require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$search = "@timberners_lee OR netneutrality OR #openinternet";
$notweets = 50;
$consumerkey = "12345678910";
$consumersecret = "12345678910";
$accesstoken = "12345678910";
$accesstokensecret = "12345678910";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$search = str_replace("#", "%23", $search);
$tweets = $connection->get("".$search."&count=".$notweets);

echo json_encode($tweets);

As you can see, the PHP is similar as before but with a new search term and updated API path for the Twitter search ( Obviously you’ll need to replace the 4x access and consumer key/tokens with your own as before. Check that the tweets are displaying before updating the JavaScript.

The Twitter Search Javascript:

Copy and paste JavaScript code here

Again the JavaScript is similar to the one we used for the user timeline with a few differences. As the search results return an array of user statuses, we need to drill down an extra level. The line ‘feeds = feeds.statuses;’ does just that.

I have added an optional autorefresh variable – if set to true, the twitter search feed will automatically refresh after the interval time defined by the refreshinterval variable (in milliseconds).

You can use exactly the same HTML structure and CSS from my custom twitter feed tutorial

– Twitter only displays searches from the past week or so. Tweets might not appear if you’re searching for an obscure hashtag, phrase or user that doesn’t tweet much.
– Twitter doesn’t make all tweets available for search. View the ‘Why am I missing from search‘ article for more info

– 04/08/2013 Added re-tweet indicator and tweet intent actions
– 19/09/2013 Added better hashtag support allowing hashtags to be used in the Twitter search query as well as key words

Creating a Twitter Search & Analytics tool »


  • Divyanshi says:

    there is in error in php file
    {“errors”:[{“code”:25,”message”:”Query parameters are missing”}]}
    can you please update your code.. i use the same code… and also change the keys but still getting the error

  • Herlambang says:

    I’m running on error :
    Fatal error: Class ‘TwitterOAuth’ not found in C:\xampp\htdocs\kejar\code\search\index.php on line 128

  • Rupert says:

    Thanks for the above… however, despite the OAuth tutorial working perfectly… when i try the above, i get an error:

    {“errors”:[{“code”:195,”message”:”Missing or invalid url parameter”}]}

    any ideas?

    My code is copy pasted from above, but changed the keys for my OAuth.

    Many thanks,


    • Tom Elliott says:

      Hi Rupert, ah – looks like an incorrect path to the Twitter OAuth library on the second line – I’ve updated the path in the search article above to reflect the one used in the OAuth tutorial if you want to try changing the path.

  • aron says:

    Hey thank you very much for this!

    I got it working here: [retracted] 🙂
    I was wondering if its possible to use a input value from html and use that as a value for the $search

    $search = ” “;

    as a result: when somebody gives a value to the input they can see related twitter feeds..
    can you help me out?

    thanks in advance


    • Tom Elliott says:

      Hi Aron, yes – would be entirely possible to base the search on user input, I would do this as follows:
      1. Create a form with input field and get the form action to post to the main page.
      2. In the JavaScript, you could then setup a new search variable e.g. var searchterm =
      3. in the getJSON call on line 28 add the searchterm as a parameter e.g. get-tweets-search.php?keword=’+searchterm,
      4. In the get-tweets-search.php, the $search variable would become $search = $_GET[‘searchterm’];
      Hope that helps 🙂

      • Mitch says:

        I tried creating the search feature but it isn’t working for me. The url is Note:I use get-tweets.php but it’s really the get-tweets-search.php. The site works fine via how you described in the tutorial.

  • Kalyan says:

    Hi , I ve followed the every step as you said. But tweets are not loading. Its showing only loading animation.

  • MATTEO says:

    excellent tutorial, thank you!
    only one question: how did you create the fade transition for your tweets on this page?
    what jquery did you use?
    can you post it?

    thank you very much!

  • Joost says:

    At first I also got the ‘missing parameters’ error.
    It seems to have something to do with your search term?

    I think you need at least 2 terms.
    For example: “1erang” isn’t working for me, but when I search for “@1erang AND #1erang” I do get results.

    • Tom Elliott says:

      Hi Joost, an interesting observation but annoyingly I can’t replicate it. When I try your example, I get plenty of tweets and have tried many variations – any chance you can reply with the code (I won’t publish). Thanks

      • Joost says:

        Hmm it may not be because of the multiple search items.
        But I seem to have problems when I start the search term with a hashtag. For example “#psv” is writing 0kb to my .txt file (I use your cache option). And “@psv” is writing 200kb to my .txt file.

        Am I doing something wrong?

  • Olav says:

    I’ve got a problem with the feed not loading.

    I started out with your first API 1.1 guide ( and this is working fine. Loading tweets without any problems
    However, when I swapped the code in get-tweets.php and twitterfeed.js for the code in this search tutorial it stops loading tweets.

    I have gone over the code half a million times, and I cannot figure out what’s wrong.

    Appreciate if you could help me out.

    • Tom Elliott says:

      Hi Olav – it looks like you’re missing the ‘autorefresh’ variable (about line 10) var autorefresh = false; – hope that helps

      • Olav says:

        Hi Tom,

        Thanks for taking a look at my problem. Sorry about the ‘autorefresh’ variable missing. Have fixed that now, but I only got it to work when I removed your code for tweet caching. I will write a new comment in that article to keep these separated.

        One small thing though. I am using “$search = “from:archetweb AND #archetweb”;” trying to show all the tweets from @archetweb and also anyone else tweeting the hashtag #archetweb. This doesn’t seem to be working right as only a few of my own tweets show up. Where is the rest of the tweets?

        Currently loading 5 tweets, even though displaylimit is set at 10.

  • hugo says:

    thank you very much for sharing.
    I was able to use
    to rotate the excellent works tweets.

    excelente aportación, muchas gracias por compartir. pude utilizar
    para hacer la trancision de los tweets

  • Taylor says:

    Thank you for this tutorial! It’s working like a charm. Any chance you could publish a version that will pull tweets from a Twitter list?

  • Serge says:

    Is it possible to pull tweets from multiple users?

  • Christian says:

    Hi, as per printing the array for the time line

    This works well on this script;
    foreach ($tweets as $item) {
    echo $item->text;

    But for some reason when on search version (i.e $search over $twitteruser) it doesnt return any results.

    Am sure its my lack of understanding arrays.
    Any help appreciated!

  • Lisa says:

    Is there a way to display 2 different usernames into one feed? I have clients with specific divisions that have their own twitter accounts but they like to combine them into one feed onto a page.

    I am not very good with php (and have some knowledge of javascript), any help would be appreciated.

    • Tom Elliott says:

      Hi Lisa, it would be possible and can see the benefits of combining feeds – not entirely straightforward though. I would be tempted to start with two PHP scripts, two JavaScript getJSON calls which put the tweets in one array and then loops through all the tweets generating the HTML that way…

  • Thanks for this useful post. For web developers who cater to clients favouring a visually cohesive website design, implementing Twitter Feed authentication can mean more work. Hence, it’s always good to have a step-by-step guide on how it should be done, as reference.


  • Jesse Luna says:

    Hi, how would I access a tweet’s expanded URL? Let’s just say I want to grab the first link on a tweet and find the expanded URL? This is what I tried, unsuccessfully:

    var mylink = feeds[i].entities.urls[0].expanded_url;

    Any help would be appreciated.

    BTW, great post, this has been very helpful.


    • Tom Elliott says:

      Hi Jesse, thanks. Haven’t tried this but looking at the tweet array structure, something more like this might do the trick? (additional ‘url’ level)
      var mylink = feeds[i].entities.url.urls[0].expanded_url;

      • Jesse Luna says:

        Hmm, things started working with the code snippet I posted above. I changed the version of JQuery then things started working. Thanks again! – @jesseluna

  • Rahcael says:

    thanks for this, I was really stuck trying to connect to the api!

    I’m trying to use the statuses/filter url

    it needs to use POST and pass through the ‘track’ parameter but all it ever returns for me is null, this is the code I am using

    $tweets = $connection->post(“”, array(‘track’=>’dream’, ‘count’=>10));

    Will it work with this do you know?



    • Tom Elliott says:

      Hi Rachael, I haven’t tried using the statuses/filter but I would have thought it was possible. I’ve seen the ‘null’ message when there’s been a malformed connection string. I would be tempted to try GET if you’re not using too many parameters. Perhaps something like the below might work? (I haven’t tested this ;))
      $tweets = $connection->get(“”);

      • Rachael says:

        Hi Tom, nope doesn’t work. from what I can see filters isn’t supported by twitteroauth. Seems most libraries don’t support it yet so think i’m going to have to attempt something from scratch! thanks anyway 🙂


  • Juane says:

    Hi! I’m trying to follow your tutorial but ím having this problem when y load the page:
    OPTIONS file:///C:/Users/Juane/Desktop/BusquedaTweets/php/get-tweets-search.php Origin null is not allowed by Access-Control-Allow-Origin. jquery-2.0.3.min.js:6
    XMLHttpRequest cannot load file:///C:/Users/Juane/Desktop/BusquedaTweets/php/get-tweets-search.php. Origin null is not allowed by Access-Control-Allow-Origin.
    Do you know what coul it be?

    • Tom Elliott says:

      Hi Juane, sounds like the issue is because the JS is trying to load the tweets from your local file system. I’d try uploading to a web server – either online or localhost

  • Azum says:

    Hi. How do I get the mouseover hover animations/buttons like in your user timeline code? I’ve tried adding the snippet from the .js but apparently I’m not quite savvy enough.

  • Azum says:

    I figured it out. I could share the code if anyone else is interested.

    • Tom Elliott says:

      Hey Azum, glad you figured it out – I still need to update the search tutorial with a few bits and pieces that are new to the original jQuery twitter feed post.

  • Azum says:

    I have another question. How do I modify the feed.js so it includes “Show this photo” links within the tweet and allow the user to enlarge the photo within the feed, basically standard widget behavior? I’m betting it has something to do with:
    //Function modified from Stack Overflow
    function addlinks(data) {

    But I’m not quite sure what to change.

    • Tom Elliott says:

      Hi Azum, might be a bit tricky to get images displayed inline but addlinks function would be a good starting point. You’ll want to find instances of and inject the HTML img tag at the bottom of each tweet. Can’t remember seeing this behaviour on many widgets/apps – usually you need to select the tweet or the image link before you see the photo

      • Azum says:

        If you create a widget under your Twitter settings, it is the standard behavior in any tweet that includes a picture. Thanks for your suggestion, will give it a try.

        • Sam says:

          Hi Tom, Azum,

          I’d also like to display the photo/images with each tweet in the feed, I’ve added the following var to the js file but I’m not sure where to go next. Great code by the way.

          var status = feeds[i].img;
          var status = feeds[i].url;

          Azum, can you share the code for the mouse over please, I can’t seem to get the retweet, favorite, etc to display? Many thanks for your help.

          • Sam says:

            Hi Tom,

            I’m also trying to add a text area so signed in members can search for a specific set of hash tags instead of having a hard coded hash tag in the php. I’ve taken the points you mentioned above on what to update but I’m stuck on the 3rd point below, I seem to get a coding error when updating the js with this line, I’m not sure I’ve got the correct code in the right place? Can you show how the code for those few lines should look? Hope you can help. Many thanks

            3. in the getJSON call on line 28 add the searchterm as a parameter e.g. get-tweets-search.php?keword=’+searchterm,

          • Tom Elliott says:

            Hi Sam,

            Sure, point 3 in the comment above – around line 28 in the twitter feed JS should look something like:
            You’ll probably also need to check that the variable searchterm is defined. If you’re still having problems, feel free to send a link

          • Azum says:

            Sure. I’ve added caching that Tom posted about as well. I also changed it so retweets show up with the username that retweeted, instead of the original tweeter. You can figure out how to change all that back if you want… I’m sure.


  • Sam says:

    Thanks Tom, I’ve updated the following:


    – line 28 is now ……. $.getJSON(‘…(my website)…../get-tweets search.php?keyword=’+searchterm,

    – under var refreshtimer; I’ve added……var searchterm = ;


    – $search = $_GET[‘searchterm’];

    form (on the main page – feed.html)

    However it’s not firing for some reason? Thanks for your help.

    • Tom Elliott says:

      Hi Sam, a couple of things – I’d first test the search variable – e.g. var searchterm = “test”; – and make sure you can query the tweets PHP with different searches from the feed JS. Once you’ve got that working, remove the var searchterm from the JS file itself and add it as inline JavaScript to the main feed page (which should be PHP rather than .htm) – something like:

      Though I haven’t tested this so feel free to send me a link if you still have trouble 🙂

    • Diego says:

      Hi Sam! For to ask you if you found the way to run the search tweets entering a hashtag from a form?

      Thanks in advance

  • Mike Dennison says:

    Hi, is it possible to combine the original Twitter user feed with a search for specific hashtags? I currently have a client who we have setup with a feed of their Twitter account on their website but they also want to add hashtags (even if they are not included on their original feed) into the feed on the website.



  • Barend says:

    Great post!

    How can I read the JSON output with a ForEach loop in PHP? I tried several things but can’t get it working…

  • Daniel says:

    Hello, nice job with the code above.
    It works perfectly with any @whatever, or any keyword, but when the $search var is like this $search = “#seo” , the response is “{“errors”:[{“code”:25,”message”:”Query parameters are missing”}]}” .

    Is there a way to make a query search using only hashtags ? Without any user tag or keywords ? Something like this “#seo OR #google OR #webevdoor”.

    Many thanks !

    • Tom Elliott says:

      Hi Daniel, thanks and cheers for pointing out this issue. Although the keyword search also searched for hashtags, it didn’t take into account specific hashtag searches. I’ve updated the post now (adding the line $search = str_replace("#", "%23", $search); ) since the hashtags need to be properly encoded for it to work. 🙂

      • Daniel says:

        Thanks for the answer !
        I was able to figure it out just after I posted the message.
        Thanks again and keep up the great work, it really helps us a lot.

  • Rodolfo says:

    Great Work…Congts..Thanks

  • Julio Sánchez says:


    until now I have reach how to show all the tweets I’ve published using mi twitter account using this code:


    But I would like to know if is there any way to show all the tweets that contains my user name @twitteruser and that have published other users.

    Thanks a lot

    • Tom Elliott says:

      Hi Julio,

      Yeah, you can use this search with the string:
      and the search parameter can contain the username which will show mentions of that user rather than tweets by that user

  • Simon Hill says:

    Would you accept a question from a non programmer? I’m designing an app for iPad and your Twitter Feed search matches very well a feature we would like to include. My question is ‘Would your tutorial work for a programmer writing software for an Apple iPad App?

    Apologies in advance if this is a dumb question. I’m guessing the software language is different, but maybe the code is a good starting point. Is it worth sharing this link to my agency as proof of concept?

    Thanks in advance.

    • Tom Elliott says:

      Hi Simon, I’m happy to help where I can and feel free to share, though I have little experience in developing native apps for iOS. If your programming the app in Objective C, the principles should be still the same. Your app could make a call to a script on a web server (which could be PHP like in my tutorials) and then parse the JSON results for tweets in Objective C and I’m sure there’s ways of doing that. Alternatively, you could get your web server script (PHP or otherwise) to generate all the HTML directly which could then pass back to the app.

      Hope that helps 🙂

      • Simon Hill says:

        Cheers Tom… I will pass your advice on. At least I know this should work in principle and provide a head start for the programmers. You certainly keep the spirit alive for sharing knowledge freely on the web! Maybe you like skiing, in which case help yourself to our free App just launched last week (only for iPad): for news on that.

  • C Row says:


    I’m trying to use two twitter search feeds on one page and can’t get either to display.

    I’ve created 2 different getTweets php files, 2 different JQuery files and directed them to display the tweets in 2 different divs.

    Any help would be greatly appreciated.



    • Tom Elliott says:

      Hi C, yeah that would be the approach I would use for 2 feeds – can you get it to work if you turn one of the twitter feeds off? If you can send me a link, I’ll find it easier to troubleshoot 🙂

      • C Row says:


        Thanks for taking a look.



        • Tom Elliott says:

          Hi C, I’ve taken a look at the link and I can’t figure out the issue. It’s like the JavaScript isn’t being run at all as there are no errors but I would test this by adding alert(); at various points in the twitterfeed.js to see if you’re getting any output. If the alerts don’t work, try completely stripping back the HTML. You’ve got a couple of jquery’s being loaded on your page for example and one might be conflicting with the other.

          • C Row says:

            Hi Tom,

            Thanks for having a look. It is a bit odd.

            The only other way I could think of doing it would be 1 script etc which searches for both terms and then in the Jquery you filter them and display them separately. But that’s beyond my (copy, paste, hope) skillset!



          • Tom Elliott says:

            Hi C, yeah – that would be a harder way for sure! I’ve seen 2 twitter feeds run side-by-side using the first method without any issues, so there will be a bug somewhere I think. If you’re still having issues, I would strip everything back and try and get one feed working at first. Maybe even use just the code from this post as a starting point.

  • Diego says:

    Congratulations for your blog, what you have shared has helped me to know something more about the twitter API and so on JS.

    I have a question, how I can do to make a list of users (25 +) search and extract the last twetts concerning a certain hashtag?. I had thought to do that for each of the users make a search, but did as an all in JSON file, then be able to present a single feed?

    Once again congratulations for your blog and sorry my English is not very good

    Regards !!!!

  • Diego says:

    Hi Tom! How could I do a search for tweets, introducing a hashtag in a text box in the index.html, and upload the results on the same page, ie in the same index.html? Something like this

    Thank you very much for the help

  • newes says:


    So there is no chance to custom search version ?

    I tried to accomplish this with you comment Tom, but there is no go 😉

  • crab says:

    hi tom
    me test :

    show with :

    foreach ($tweets as $item) {
    echo $item->text;

    it work and tnx

    but me use :

    work and show with:
    echo json_encode($tweets);

    but not show with :

    foreach ($tweets as $item) {
    echo $item->ids;

    Why result empty ?

    • Tom Elliott says:

      Hi Crab, not used the followers/ids Twitter endpoint before but it’s likely that it uses a different structure which is why you’re seeing an empty result.
      I’d use print_r to see how this is structured, e.g.
      foreach ($tweets as $item) {

  • Lara says:

    Hi Tom and thanks so much for this tutorial.

    Is it possible to search only geolocation tweets and extract the coordinates to the tweet?

    • Tom Elliott says:

      Hi Lara, a good question! It looks like, if a user has tweeted with geolocation enabled, then we can get the co-ordinates from the returned JSON of the search. e.g. you can see the below if you use print_r($tweets);
      [coordinates] => Array
      [0] => 53.49034345
      [1] => -1.38878296

  • Diego says:

    I have a question, how could save an array of each tweet hashtags?. I have seen that are stored within the json in “statuses.entities.hashtags”. Here should go each index ([0] [1] [2] … [n hashtag]) and go stored in a variable, right? How could I solve it?

    Your help is invaluable

  • biz patrick says:


    Where you copy and paste php code, before head or in the body,
    because JavaScript code don’t work, on my page but it’s ok
    for print json_encode


  • richard oliveros says:

    good afternoon, thanks for the code, I want to just throw a few hashtags counter there, how could it?

  • I still GET 500 Internal Server Error

    I’ve given the relative path and have the twitteroauth library from >

    Anyways, suggestions on passing the json to javascript using jquery?

    • Tom Elliott says:

      Hi, yeah an Internal Server error can be for all sorts of reasons – the path issue being one of the more common. If you can turn on friendly PHP errors or find the error in a log file, that should give you a much better idea what the problem is.

  • Brad Greenfield says:

    Thank you for the tutorial. Very straight forward way of doing things. I am using this for a conference app I am building for UT and looking for a way to post from the app to twitter. I would ask for one as clear as your tut, but that may be asking a little much. Any ideas?

    I hope one day to be able to offer tuts for people like me that spend countless hours searching the interwebs for things just like this.

    • Tom Elliott says:

      Hey Brad, thanks for your comment. I’ve posted scheduled tweets (tweetbot) before but used a different API (TwitterAPIExchange). Might have a tut up about this one day but I reckon this tutorial could help 🙂

  • Ben says:

    Thank you so much for this tutorial, helped me understand a little more…but unfortunately, I get this as a response:
    {“errors”:[{“message”:”Could not authenticate you”,”code”:32}]}

    My keys are good, double/triple checked…

    Do you have any idea of what it could be?

    Thanks alot and have a great day!


  • Rick says:

    Hello Tom,

    I am able to get the search-tweets1.1.php file to display the raw output for my search terms, but I am unable to get the final css-styled page to display anything other than the blank white css container with ‘Custom Twitter Search’ as my header. All of my urls seem correct. Any ideas?