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

18 February, 2013 by Tom Elliott

This is comment page 3 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

  • ikhsan says:

    thanks a lot πŸ˜€

  • vimal says:

    hi tom, its not working for me

    its my link

    • Tom Elliott says:

      Hi Vimal, yeah – you need to change the path in the twitter feed JS to the get tweets path on your server (at the moment you still have the webdevdoor link) πŸ™‚

  • Trynda says:

    This is the second tutorial i’ve looked at and this one seemed to be way more helpfull than the first (At least i’m getting some sort of error) I changed my get_tweet.php file to have my codes and have everything in the right directory, but i’m still getting a white screen with “null” in the top left corner. My server should be running PHP 5.2 and my php file does not have a callback argument in it. I’m not sure what else might be wrong.

    • Tom Elliott says:

      Hi Trynda, I’d double check your PHP version but 5.2 should be fine. Are the only parameters in the $connection->get line the twitter user and number of tweets?

      • Trynda says:

        Yes I am only using the username and number of tweets in the $connection-> get. My website is hosted at http://www.000webhost.com/ who claim to be running PHP 5.

        • Tom Elliott says:

          Hi Trynda, it’s worth running the php command phpinfo(); which will tell you exactly what version of PHP 5 the server is running as you need 5.2.x This should also tell you if cURL is enabled which is required for the authentication library.

          • Trynda says:

            I’ve run phpInfo in my script and I can see cURL is enabled and the PHP version is 5.2.17.

          • Tom Elliott says:

            Hi Trynda, is there any chance you can send me your code? I will test it on my server and will help identify if it’s a server issue or something in the code. I won’t publish the keys or anything πŸ™‚

  • Raj says:

    Hi – thank you for the tutorial – works excellently!
    My question is that – how can I add two domains to make it work?
    For instance www. – works and without www does not work.

    Thank you – apologies if someone has already answered this, I couldn’t find the answer!

  • Jignesh says:

    Hi i have used this library but some how i got error on OAuth.php on line no 386. there is port @$parts[‘port’] is there . it is showing me null when i comment it and its repective code line then no error will occur but i got null as a out put. it is not workig for me. can any one tell me what to do now. Please help me.

    • Tom Elliott says:

      Hi Jignesh, not an error I’ve seen before and never needed to modify OAuth.php. I would check your PHP version and make sure you’ve got cURL enabled. The twitteroauth library requires PHP 5.2.x

      • Jignesh says:

        Hey Tom,

        thanks for helping me. cURl is enabled. now i got null error. how to solve this.

        • Tom Elliott says:

          Hi Jignesh – what does the get tweets “connection->” string look like? I’ve seen a null error before when there’s been a callback parameter in the string so make sure only the twitter username and count parameter are in there.

          • Jignesh says:

            Hello Tom,

            I have checked it. there is no extra parameter in β€œconnection->” string. There is only twitter username and count parameter are there. I don’t know how this is possible. is there something other settings do i need to do ?. I can give you the link if you want to check. right now i have make widget from my twitter account and place the code on the site. so widget is working now. but application code which i have set up on developer https://dev.twitter.com/apps is not working.

          • Tom Elliott says:

            Hi Jignesh – it sounds like you’ve got everything in place – any chance you can send me your full get tweets php code? I won’t publish the comment or you can send it through the contact page if you want. If I’m able to get it working on my server then it would indicate a server setting but if I get null, hopefully I can find the problem πŸ™‚

  • Jignesh says:

    Hi Tom,

    I am stuck in this API integration. i am integrating it with opencart. can you tell me what to do. i need help. i am in trouble.

    • Tom Elliott says:

      Hi Jignesh – before trying to integrate with opencart, I’d try and get the tweets php file working – are you still getting the null error? I don’t use opencart so I don’t think I’ll be able to help you with the integration. Once you’ve got the twitter stream outputting the tweets in JSON, you can parse them using JavaScript and my jQuery article should help with this.

  • Thomas Reggi says:

    I’m getting {“errors”:[{“message”:”Internal error”,”code”:131}]}.

  • Abhijit says:

    Just Brilliant!! Wonderful!!! Thank you

  • safouane says:

    hey, is there a similar tutorial for Java

  • Naveen says:

    Thanks for the tutorial – quick question, is it possible to use javascript for this new API as I need to embed the twitter timeline on a html page.

    • Tom Elliott says:

      Hi Naveen, yes it is possible to use JS libraries for twitter feed authentication (there’s a couple on this page) but Twitter recommend against this due having your keys/tokens visible to anyone. If you have access to another server with PHP setup, another potential solution could be to get the authentication script working on that server and use a cross-domain JSON call.

  • dan says:

    Hi
    Thanks for the tutorial but I can’t get this too work, it seems to be showing info but I must have missed something. This is the link with my problem, any ideas?

    Thanks in Advance

    Dan

  • suhendra says:

    Hi Tom,

    thanks for this awesome tutorial.
    but I’m getting response :

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

    Here’s my code :

    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;
    }

    public function twit() {
    $twitteruser = “cuheguevara”;
    $notweets = 30;
    $consumerkey = “myconsumerkey”;
    $consumersecret = “myconsumersecret”;
    $accesstoken = “mytoken”;
    $accesstokensecret = “EY9JhbtBCjXux40ieJh5TsnRqjscBnQ0Jii5urk9c”;

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

    $tweets = $connection->get(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=” . $twitteruser . “&count=” . $notweets);

    echo json_encode($tweets);
    }

    Any Help ?

  • Suhendra says:

    Hi Tom.

    Great tutorial. now my code is worked.
    I create new application πŸ˜€

  • Dave says:

    Hi Tom

    Thank you for the fantastic tutorial this has been a great help. I have got everything working fine using the tutorial code to display a static feed. I haven’t used the caching feature as the site has very low traffic levels.

    In an earlier post you made reference to a javascript file that used animation.

    https://tomelliott.com/demos/jquery-twitter-feed/js/twitterfeed-main.js

    I have looked through the script and this code from line 21 or so seems to point to a cached txt file of feeds.

    $.ajax({
    url: ‘/demos/jquery-twitter-feed/’+twitterprofile+’-tweets.txt?’+Math.random(),
    dataType: ‘json’,
    success: function(feeds) {

    I have tried to edit this script using the code from the static js provided in the example to take the json from the php file as follows

    $.getJSON(‘http://www.advmultimedia.co.uk/twitteraut.php’,
    function(feeds) {

    This however generates a syntax error on line 105

    error: function(jqXHR, exception) {

    Now I think the error is because I have omitted an opening “{” with the replacement tag. I have tried and failed to remove the error and cant get it to work for the life of me.

    Can you advise on what code would need to be entered to enable the php authentication file to provide the json information for the feed to allow the animated version to work?

    Many thanks

    Dave

  • Neil says:

    I am getting an error – Internal Server error.
    I have checked my path to twitteroauthphp and that is correct. My server is running php 5.3. my path to get-tweet1.1.php is correct. I am really stumped. In the get-tweets.php file on line 19 ( $tweets = $connection->get(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”.$twitteruser.”&count=”.$notweets); ) does scree_name need to be changed to your twitter username. I am just at a loss.

  • Matt says:

    I have gotten this code to work, but I am having a problem putting together the proper flow for the following behavior:

    I want to pull messages from a database table and automatically post tweets in behalf of users that have authorized my app to do so.

    In order for that to happen, I need to login the user without a browser since I want to trigger this script with a cron job. Has anyone seen a solution for this?

    I am been able to post tweets for my own account by using the access token and access token secret data. But, working out a way to get these for a user that has authorized my app read and write access has proved challenging.

    Thanks in advance for any help.

    • Tom Elliott says:

      Hi Matt, I can see what you’re trying to do. I haven’t done any OAuth posting either from my own account or on behalf of users so I don’t think I’ll be able to help much here. Hopefully this link will help you in the right direction though: [link outdated]

  • Aden says:

    Thanks a lot, your post was very very help full.

  • Sabby says:

    Thanks for sharing this code.
    It really help me to display tweet.
    I have one question how do I pull usermention. I visited this link https://dev.twitter.com/docs/api/1/get/statuses/mentions but I am not able to find.
    Could you help me something.

  • Moin Choudhari says:

    Hi Tom,

    Thank you for the script. It proves much useful in my project!

    The only thing I found that the tweets that are displayed are not the most recent. The displayed tweets are older than the most recent one.

    Any help would be highly appreciated πŸ™‚

    • Tom Elliott says:

      Hi Moin, that’s odd – the most recent tweets should be at the top (I tweeted a few minutes ago and it’s popped up) have you got a link you can send me? πŸ™‚

  • Lionel says:

    It helped a lot, thank you!

    PS: It’s not a big prob but the path to twitteroauth library is “twitteroauth/twitteroauth.php” and not “twitteroauth/twitteroauth/twitteroauth.php”. πŸ˜›

  • brady says:

    Hi,

    I am also getting the null return from the get tweets php script. It is working perfectly on my testing instance (5.3.2), but when I migrate it to the production shell (project is hosted from sourceforge), the script returns null:
    http://www.open-emr.org/twitter/tweets.php
    Here is the php version info (it appears version should be good and curl is activated):
    [removed]

    Let me know if you need to see the php script (it is basically copy/pasted from yours and again works perfectly on my local testing instance).

    Happy to debug this if needed with some guidance since main project page is a bit dysfunctional at the News section:
    http://www.open-emr.org/

    thanks,
    -brady

  • Marina says:

    Hi, I had it working on my websites for months, until today… all of them chased and doesn’t load the timeline, it’s frozen on the loading gif.

    The error I get is “Uncaught TypeError: Cannot read property ‘length’ of null”

    Any idea what it could be causing this problem? I already updated the jquery version to 1.10.2, and I saw your Troubleshooting tip 4 but couldn’t quite understand it.

    • Tom Elliott says:

      Hi Marina, what happens when you try and access the PHP file directly, does that still load the tweets? Are you aware of anything that might have changed on the server? If you can send me a link I’ll try and take a look πŸ™‚

      • Marina says:

        Hi Tom, it’s back to normal now, go figure. I guess it was some problem with the host provider. Thank you so much for the great work and support.

  • Christian says:

    I am repeatedly getting an Internal Server Error, and I’m not sure how to make the filepath not relative as I am being hosted on a hosting service. Any solutions to this?

    • Tom Elliott says:

      Hi Christian, you could try checking your log files or enabling friendly PHP errors as an internal server error can mean a number of things, however the problem is often with the path to the twitteraouth.php file which does need to be relative.

  • John says:

    Excellent tutorial! I’m currently going through a WordPress Plugin tutorial at https://tutsplus.com (how to use Twitter API), and I almost gave up on that lesson since it was using the old API method.

    After seeing your blog and applying the instructions, the twitter API works like a charm!

  • Jon says:

    Hey Tom!

    Amazing tutorial, so up to date too, which is not something I can say for any other API tutorials I’ve found online.

    Just a quick question (and sorry if it’s been answered above) but how could I use this to track a specific hashtag?

    Thanks again!

    Jon

  • Kenny Landes says:

    After banging my head into the wall all day trying to decipher Twitter’s documentation to conjure up some magic, this got me there in less than 30 minutes. Much appreciated!

  • kliMa says:

    {“errors”:[{“message”:”Sorry, that page does not exist”,”code”:34}]} in get-tweets.php – what can be a reason of that ?

  • kliMa says:

    if you can’t connect, change this line with your proper “screen_name” in get-tweets1.1.php

    $tweets = $connection->get(“statuses/user_timeline”, array(“screen_name” => “screen_name”, “count” => 10));

  • jordy says:

    Hi Elliot

    You helped me a little while ago make the twitter feed responsive and i was able to get that done, which is great. but I wondered if you could help me one more time…

    I am trying to get the feed to work on this page http://tanisharose.com/beta/lyrics/jezebel.asp but I keep getting this error: error – Internal Server Error

    At first I thought it was because I was referencing the twitter php and js files outside this folder, but I created the same structure inside the folder and still no go.

    I cant figure out what is wrong because its set up the same one level up. Any help would be greatly appreciated.

    Cheers

    • Tom Elliott says:

      Hey Jordy, yeah – it’s the php file that is giving the server error which can be down to a number of things. Quite often I’ve seen an absolute path used to the twitteroauth library when it should be a relative path. If that doesn’t work, try turning on friendly PHP errors to troubleshoot or check your log files which should point to the problem πŸ™‚

  • Jev says:

    Hi Tom this is an awesome article. I just have a problem :/ I’m getting a blank white div. Maybe you can help me? I read troubleshooting tip 3 but I don’t know what relative path means (I noob) Thank you very much πŸ™‚

    • Tom Elliott says:

      Hey Jev, is the white div after you’ve tried to add the JavaScript for the feed? This could be a path issue but if you can send me a link I’m happy to take a look πŸ™‚

  • Eric says:

    I got it to work… Mostly. No tweets are coming through the box. What have I got wrong here? Your help is greatly appreciated. πŸ™‚

    Thank you!

  • JC says:

    Hi Tom,

    I had a simple C++ app which used to get information from Twitter (in JSON format) thru the string like : “http://search.twitter.com/search.json?q=HOLLYWOOD&show_user=true&result_type=mixed&page=15&rpp=100” .

    Now it has stopped working as twitter has migrated to v1.1. I can understand the fact that I have to include the Authentication stuff here in my string request. I am yet to create my Auth details in Twitter, but I am unable to find anywhere in the internet as to how I should pass those Auth details in my string request. I don’t have any knowledge in Java or PHP. I was not able to find it in this site also, or maybe I am doing something wrong. So would you kindly help me and guide me here???

    Thanks in advance,

    • Tom Elliott says:

      Hey JC, I’m afraid I won’t really be able to help you with C++ as I haven’t used it (at least not for 10 years!) but the process should be the same as in this tutorial except you might want to use a C++ library instead of PHP. I haven’t used it but Twitter point to a C++ library called twitcurl which might be worth checking out https://code.google.com/p/twitcurl/ πŸ™‚

      Hopefully there’s a demo/example script that you’ll be able to modify here.

  • Eric says:

    Thanks very much, there were some definite issues there, a bit better bit still no cigar… The path to twitteroauth.php seems correct, but still I dont get anything… What could it be?

    • Tom Elliott says:

      Hi Eric, yeah you’re now getting a completely blank page in your PHP. Are you using a relative path to the twitteroauth? Also check you’re running PHP 5.2.X or higher

  • Eric says:

    the path is [removed]

    which is the correct path for just about everything else… I noticed that the php file came up blank, too.

    Total noob here sorry. I don’t think I am “running” php 5.2.x or higher, I wasn’t aware – so more than likely I am not. Hopefully that is the problem. Thank you for helping me narrow this down. I’m really hoping that’s it! Maybe there’s a quick link you have to inform me? I think I will also do a search my self.
    Thanks again πŸ™‚

    • Tom Elliott says:

      Hi Eric, yeah actually it’s normal for the twitteroauth.php to come up blank – you just need to make sure the path to it is not absolute e.g. require_once(“twitteroauth/twitteroauth.php”); should be correct in your case.

      You can use phpinfo() to see what version of PHP you’re using πŸ™‚

  • Eric says:

    Aha… I see what you mean about the absolute path. It’s working now, except all the tweets are jammed up at the top. I’m currently seeing if it’s a styling issue or maybe something in the javascript. Thanks again Tom, you’ve been a great help so far.

  • Ollie says:

    Thanks for this Tom – but its not exactly beginner friendly with all the chat about php files and uploading to servers etc… is there anyway to get around all this hoopla?
    Step 2 is just a bit much for the humble wordpress newbie. Any suggestions? Seems a bit much for a twitter feed…
    Thanks,

    Ollie.

    • Tom Elliott says:

      Hey Ollie, there are a few other options available including the Twitter Embedded timeline and WordPress widgets (although haven’t tried any) as outlined in my Twitter options post if you need more of a beginners solution πŸ™‚

  • Rule says:

    vi your php.ini

    search for curl & remove curl_exec
    Restart apache.

  • rwdmac says:

    how can I get this to work with a wordpress theme that is giving me this error message, the documentation is pointing to this site for reference in setting tweets up.

  • Paul says:

    Just got this working in 20 mins on a new site we are developing, looks and works great http://www.burgerland.co.uk

  • John Roberto says:

    Tom,

    thanks so much for this walk through. I am very new to PHP and seem to be running up against something odd. When the script runs, all I get is a page with the actual PHP from Abraham. My understanding is that this means PHP isn’t enabled on my server but it very much IS enabled (5.4).

    1&1 is my hosting provider, wondering if it could be an issue with them…

    • Tom Elliott says:

      Hi John, that does very much sound like what happens when PHP isn’t enabled. Maybe you’re hosting package says PHP 5.4 is available but somehow needs to be activated? Unless all the PHP symbols have been converted somehow to it’s corresponding HTML codes. e.g. the ‘<' characters have been converted to '& lt ;'