Authenticating a Twitter Feed for OAuth API V1.1 - Timelines & streams

18 February, 2013 by Tom Elliott

This is comment page 4 for Authenticating a Twitter Feed for OAuth API V1.1 - Timelines & streams »


Creating a Custom jQuery Twitter Feed (API v1.1) »
Creating a Twitter Search & Analytics tool »


376 Comments

  • sukh says:

    hi tom.

    i have created app on twitter.
    my website is ready.
    i also created php file.
    but i dont know how to add these all things to my home page and where to add in html.and how to call these all things.
    thanks for your help.

  • I’m having a problem with PHP files and twitteroauth. can i use dropbox public folder?

  • Scott says:

    Hi Tom,

    First, thanks so much for all your work on this. Seems to be the only working sample I have found.

    I am getting some crazy output though –

    Notice: A session had already been started – ignoring session_start() in /home/westonma/public_html/templates/_inc/news.php on line 21
    [{“created_at”:…

  • GreysonP says:

    This was so insanely helpful. Thank you so much!

  • Perumal says:

    Hi,

    thank you the great info…
    i’m using this code but i couldn’t get my feeds, when login my account partially redirected my authentication URL…
    what can i do for this config.php file on OAUTH_CALLBACK url…
    i’m given the below file url name:
    define(‘OAUTH_CALLBACK’, ‘http://mysummary.me/for_appln/twitter/my_twit1/twitteroauth-master/index.php’);

    please guide me..
    thanks and advance
    Perumal

    • Tom Elliott says:

      Hi Perumal. I’m not sure where you’re getting the define(β€˜OAUTH_CALLBACK.. line from? You should just be able to upload the twitteroauth library and use the code in the post

  • Tom says:

    Hi, cheers for the excellent tutorial.

    Got a bit of an error going on here and I can’t quite work it out. I can get the PHP call to work fine (I get a massive amount of JSON garbage), but when it comes to putting the jQuery shine on it, I get the following error.

    On the firebug console, it says:

    TypeError: feeds is null

    for (var i=0; i<feeds.length; i++) {

    When I uncomment the "alert(feeds)" line above that, it's just a null alert.

    Can you help please? Is it something I've missed?

    Cheers,
    Tom

    • Tom Elliott says:

      Hi Tom, hmm, sounds like a parsing issue – have you checked the PHP to make sure there’s no HTML being output either side of the feeds PHP? If you have a link you can send me I can take a look πŸ™‚

  • Tom says:

    Wait…. hold on.

    You said nothing in the Tutorial about quoting out echo json_encode($tweets), didn’t you?

    Christ, I’m a complete moron.

  • Tom says:

    OK, it’s all working now.

    For those who are stupid enough to make the same mistake I did: Don’t quote out the last line of php. (the echo json command).

    Sigh. I need another coffee.

    Cheers for the excellent tutorial Tom!

  • jean says:

    hi,

    it helped me alot but in some parts I am stuck :S !!! still did not get it very well πŸ™ !!! from the step 2 when I have to create php file then what ??

    can u explain it in a simple way ??

    Thanks alot

    • Tom Elliott says:

      Hi Jean, once you’ve downloaded the oauth library, created the PHP file with your keys, you then need to upload it to a web server – either online or locally that’s running PHP.

  • phil says:

    Since june I used the new version of twitteroauth and all worked fine but since one week, it’s impossible to send any update.
    I have the ‘null’ answer.

    I’ve tried with your code, update the php version, check for curl … no change
    Any ideas ?

    • Tom Elliott says:

      Hi Phil first thing I’d be tempted to do is try your code on a different server or locally – this will eliminate a potential issue with your keys, app, or the code itself. If it works, then there must have been some change on your server – your hosts may be able to help pinpoint this. I’ve recently updated troubleshooting tip #4 which might help with a php.ini setting – other than that, if you’re running PHP 5.3.x or higher, try checking your server firewall to make sure Twitter isn’t being blocked or check the Twitter url path in your get tweets file to make sure there isn’t a callback parameter.

  • Jordi says:

    THANKS!! You’re a God for me right now!

  • Nathan Grant says:

    Hi Tom,
    I’m getting “tweet loader” after following the instructions.

    • Tom Elliott says:

      Hi Nathan, I think “tweet loader” is just the alt tag. Your Get-Tweets1.1.php file is throwing an incorrect path error to the twitteraouth library. I’d also try and fix the other JS errors on your site as well.

  • teja says:

    Hi,

    I am getting the following error when running the php script. I am using xampp.

    {“errors”:[{“message”:”Invalid or expired token”,”code”:89}]}

    Can someone please help !!

    • Tom Elliott says:

      Hi Teja, I don’t think I’ve seen this error before. Try re-copying your twitter app Auth keys and tokens, maybe pasting into notepad first to make sure no hidden characters are copyied over. Failing that, you could try re-setting up your app in Twitter.

  • bhanu says:

    Hi Tom,

    First of all I would to thanks you for such a nice tutorial. I have followed the exact instructions given and am getting the error

    []

    when I execute the PHP. I am using xampp. I checked the php.ini file and other settings mentioned in the comments above and everything looks good. I am just no sure of the relative path property. Could that be the issue?

    Thanks once again.

    • Tom Elliott says:

      Hi Bhanu, it looks like the error didn’t come through in the comment correctly, unless all you are seeing is the square brackets? In which case I haven’t seen this before. The relative path should be the path to the twitteroauth.php file depending how you unzip the library files. (It’s usually “twitteroauth/twitteroauth/twitteroauth.php” or “twitteroauth/twitteroauth.php”). Also check the version of PHP you’re using. It should work with PHP 5.2.x and above but a number of issues can still arise with 5.2.x

  • Amit says:

    Ahhaaa finally i am able to fetch tweets πŸ™‚ Its still confusing for me to differentiate consumer keys and oauth keys, but no worries, Its working now.

  • bhanu says:

    Hi Tom,

    I got the app to work at last. Thanks for the great tutorial. I want to add some other functionality as well. Do you know how I can create a json file with the response instead of just echoing it and seeing the results in the browser?

    Thanks,
    Bhanu.

    • Tom Elliott says:

      Hi Bhanu, thanks. Sure, you can check out my tweet caching code for that. It basically goes through the code required to output the tweets as a file instead of echoing them. You can just use the .json extension instead of .txt, although some servers need .json setting up as a MIME type. Step 2 caching is useful to keep the .json file up to date.

      Hope that helps πŸ™‚

  • matt says:

    Thanks a lot

  • Adam Willard says:

    Is it possible to have two twitter feeds on the same page, one from the account and another from a #hashtag? If so how would you go about this.

  • Caroline says:

    Just to say, thank you, this has been a major time saver! I was cursing Twitter for changing everything but this is really simple to implement and a really written tutorial. Thanks again

  • Obc says:

    Hi Tom and all!
    Thanks for the post and your availability!!!
    Works for me in my real server.
    Anyone know how to configure XAMPP to allow seeing tweets?
    XAMPP returns error “exception is not defined”.

  • Sarah says:

    Hi Tom, I spent all day looking at different twitter plugins that would create a custom feed on my client’s website. And I knew about yours from the beginning but I went into the forest of dead twitter plugins anyways. There’s some interesting ones.

    Anywho, I’ve followed the tutorial and my client’s feed does not show up at all in the twitter-feed div. I read all the comments and perhaps it could be my client’s webhosts PHP version being too old? The version is PHP 5.3.3

    I also saw that a blank page could be a setting in the PHP.ini file with curl being disabled, but the PHP.ini has no disabled_functions. So that’s not it.

    Do you have any ideas on were I might have went wrong or why the feed is not showing up?

    Thanks for your great efforts.

    Sarah

  • Nathan says:

    Tom, I’m getting a bad authentication data error. Any thoughts on this?

  • Davor says:

    Hi,

    I try to add this twitter script to my wordpress theme and everything work good.. but I can not add username from theme options to get-tweets1.1.php .. Anyone can help me?

  • Vasiliy Belous says:

    Warning: session_start() [function.session-start.html]: Cannot send session cookie – headers already sent by (output started at /home/student/belousv01/public_html/475/hw4/index.php:67) in /home/student/belousv01/public_html/475/hw4/index.php on line 72

    Warning: session_start() [function.session-start.html]: Cannot send session cache limiter – headers already sent (output started at /home/student/belousv01/public_html/475/hw4/index.php:67) in /home/student/belousv01/public_html/475/hw4/index.php on line 72

    how getting these errors

  • im getting a NULL as well. i know that it hasn’t have to do with the curl. because i used it before for my Instragram API on the same php file and it works fine with instragram.
    Any ideas what be causing the NULL ?

  • Hey, im Getting NULL errors. I am running php version 5.2.14.
    I know that CURL works because i used it for my instragram API. I checked my Get() as well i cant find anything wrong. maybe you can take a look ?
    Please Help me because i need this to work to complete my project. that’s due on the 10th

    • Tom Elliott says:

      Hey Vasiliy, I’ve seen Null errors before on older versions of PHP (before 5.3) – I think this is your most likely problem. Also checkout Troubleshooting tip 4 if you haven’t already

  • Neil Clarke says:

    Hi Tom
    Firstly thanks for an awesome script, your instructions are superb. I’ve followed it all and the feed works fine, but from time to time (about 20% of the time) I get the following error: ‘connection problem. Check file path and www vs non-www in getJSON request.’
    Is it because I’ve put the js in the head of the document and users are trying to navigate the site before the feeds loaded?

    Many thanks
    Neil

  • Harry says:

    the fading tweets post is not working for me..why oh why πŸ™

  • miguel says:

    hi tom – really great you helping aspiring developers like myself =)

    quick question – per your instructions, ‘along with the twitteroauth library to a folder on your web server’

    if im using symfony, which specific files do i need to load on my web server? im assuming all the php files under src/entity?

    thanks dude

  • Joost says:

    Is it possible that this tutorial doesn’t match with this new update?
    [link outdated]

    My Twitter feed broke as of today, and I am only getting ‘null’ when running get-tweets.php.
    It seems to be connecting over https (if I check the code), but I don’t know if that is enough.

    Any ideas about this?

    • Tom Elliott says:

      Hi Joost, yes I read about this as well. All calls to Twitter API should be made with https. I think some people having the ‘null’ issue were able to fix it by going through ‘http’ but this is no longer an option now. What version of PHP are you using? I think I’ve only seen ‘null’ issues for older versions

      • Joost says:

        I’m using a older version of XAMPP (I think 1.7.3), so it definitely could be related to my php version.
        The strange thing is that my feed is working again today. I have know idea why, because I didn’t change anything. But the ‘null’ requests are definitely gone.

        • Tom Elliott says:

          Hi Joost, hmm that’s a little odd then. One thing to check is that the notweets variable is set quite high in the PHP as direct tweets can have an effect and are ignored by default

  • Alexandre says:

    Hi, nice tutorial, I wanna know that if this limit of 180 per 15 min is based on each user who signed or for all the users of that application

  • Dirk says:

    Typical that this doesn’t work for me. As far as I know, I followed this tutorial correctly. But when I want to fetch my tweets I get no output.

    I do hope you still read the new comments and I hope you can push me in the right direction.

    Kind regards,
    Dirk

    • Tom Elliott says:

      Hi Dirk,
      A couple of things – It’s best to have the main html file outside the TwitterOAuth folder. Also, I was getting a 404 error with the JS twitter feed file.. Tom

  • Wesley says:

    Hi Tom,

    I was able to get some information to post to my site, but I’m not sure what to make of it. I don’t know if this is actually the expected output or not, but each of my tweets is preceded with “Twitter for iPhone“,”truncated”:false,…….” I just don’t know if this is random mess of data is what I should be using to style. Link to the code is here… [removed] I also included phpinfo(); at the bottom so you can see my info. Any help is appreciated!!!

    • Tom Elliott says:

      Hi Wesley, yes this post does just focus on authentication and basic output and the mess of data is the raw json display. If you want to style the feeds, you can checkout my next article on creating a custom twitter feed. It’s also a good idea to save the PHP that generates the tweets as a separate file so it doesn’t interfere with your main pages. πŸ™‚

  • ashirwad says:

    Hi Tom, How can this be done using asp.net instead of PHP script?

  • Ana says:

    Hi Tom, thank you for the code, I’m impressed by the explanaition you gave that enabled me to make it work.
    I’m using your code in a student project, where I additionally need to save tweets to database (actual tweet + screen_name). I’ve managed to access the text of tweet with this code:
    statuses as $trend )
    {
    echo “{$trend->text}n”;
    }
    ?>
    but I can’t access the screen_name. I’m completely new to JSON, not sure how to request it (asking for screen_name instead of text didn’t work).
    Hope you can give me some hints:)

  • Nitya Maity says:

    Thanks for this post. It helped me a lot and saved my time.

  • Santosh says:

    hey there,

    I’m getting this error when I’m trying:
    Parse error: syntax error, unexpected T_VARIABLE in C:localhostwwwtwitter-1get-tweets1.1.php on line 6

    the path to the twitteroauth file is all fine but still I’m facing this problem.

    • Tom Elliott says:

      Hi Santosh, is there any additional HTML being echoed in your get tweets php file? Even a blank space caused by an extra line for example after the closing PHP tag can cause this parsing error.

  • Matti says:

    Hi!

    I’m getting this {“errors”:[{“message”:”Invalid or expired token”,”code”:89}]} but tokens are correct.

  • Sean says:

    Getting the follow error when trying to run script

    {“errors”:[{“message”:”Could not authenticate you”,”code”:32}]}

  • hakhan says:

    First of all this is great tutorial, thank you very much. Second; I read that this method must return up to 3,200 of a user’s most recent Tweets but i have just received around 190-198 tweets. What is the problem ? what do you think ?