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

18 February, 2013 by Tom Elliott

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

  • DC says:

    The quote issue is PHP based not browser based and is dependent on how its been set. I find that on most PHP installs this is a problem and we need to do some form of stripslashes if magicquotes is on.
    I think its been removed 5.4 and higher.

    Do you have any type of starting point for parsing through the data sent? so i can grab the date.

    I will try to see if i can set up some type of regex to parse the date but not to sure. have to think about that one, as i really want that formatted server side as I have done in the past.

  • Adam says:

    Hi Tom,

    Firstly thanks for a great post, really helpful. I’m unfortunately one of the select few getting ‘null’ returned. I only get null on the page and nothing else, any ideas why this may be? I checked the path to the twitteroauth.php file and that looks fine, so I’m not sure what the issue may be.

    Adam

    • Tom Elliott says:

      Hi Adam – yes, a few people are getting null but I haven’t been able to replicate it yet – can you reply with your PHP code (I won’t publish it). Also do you know what version of PHP you’re using?

  • Marinka says:

    Thank you very much for this great explanation and telling me about twitteroauth by @abraham. It works like a charm. I followed the PHP way with:
    foreach ($tweets as $item) {
    echo $item->text;
    }

    I’m very impressed at the time you give everybody helping them with their problems. You are pure gold!

    XXX Marinka from Amsterdam

    • Tom Elliott says:

      Hi Marinka, your welcome and many thanks for your comment – things have been pretty hectic on my blog since Twitter shutdown the old API and I try and help people where I can 🙂

  • DC says:

    I wrote this to replace the date at PHP lev and it almost works but not quite as the breaks i think prevent it from working correctly, so it will only replace some instances not all … Ideas on how this can be fixed hers the func I wrote in an attempt to pull off the date parse trick. try this if you would and see if you can maybe get this to work correctly.

    echo date_replace($tweets);

    function date_replace($json_arr){
    foreach($json_arr as $item){
    $obj = stripslashes($item->created_at);
    $twe = date(“F j, Y”, strtotime($obj));
    //$twe = timesince(strtotime($obj),strtotime($obj));
    $out .= str_replace($obj, $twe, json_encode($json_arr));
    }
    return $out;
    }

  • Marinka says:

    Maybe some bits of my script will help? http://www.tag-tonic.nl/twitter.class.txt

    (I can understand if you don’t want to post this because of the link)

  • Thanx, for this help…….thanx a lot…………..

  • Adrian says:

    Thanks for this, just been able to fix the broken feed on my site in less than 10 minutes!

  • Someone says:

    Awsome, thanks man!!!1one

  • kubilay e. says:

    thank you! since v1.1 required auth for even to fetch tweets from a public timeline, I was going crazy. thankfully I’ve come to found your page.

  • Luke says:

    Fixed the blank screen issues for those users who have it?

    Probably have this error

    Cannot redeclare class OAuthException in OAuth.php on line 8

    Simple rename of class in 13 locations on current document, use search and replace

    i used the name OAuthExceptiona,

    and hoorah,

    get-tweets.php works

  • dot says:

    Hi,

    I am creating an app using javascript and Jquery mobile, will you be able to direct me where I could find oAuth and getting twitter feed example using javascript? and not php.

    Thanks

  • JustVibe says:

    HI

    I cant seam to get this to work. could someone have a look at the website and give me some help

    Thanks

    • Tom Elliott says:

      Hi, your path link to the get-tweets file is incorrect. You also have an error for DDSlider

      • Daniel says:

        HI Tom

        Sorry I see what you mean now. could you have a look again and give me some help ref, styling it to show 2 tweets and to make it fit and look nice.

        have a look and you will see what I mean.

        Thanks again

  • Max says:

    Thank you!

  • Thanks for your great script, which works well.
    Found a problem, it doesn’t like a twitter user name that starts with a underscrore – e.g. _twitteruser
    ???

    • Tom Elliott says:

      Hi Andy, hmm – that’s a new one! Underscores *should* work OK – I’ve tested it with username _cdja for example. Maybe there’s a problem with a specific user timeline which just happens to have an underscore..

  • Zippy says:

    Hi Tom, thanks for Ur tutorial.
    I have follow Ur tutorial step by step, but how to use this script in my sidebar.
    Yeah, I want to add my latest tweet in my sidebar.
    In Twitter API v1.0, i use this script:

    Please, help me. Thanks.

  • Martin says:

    Thanks.But i have everything well set and getting ‘null’ on my page.

  • Daniel says:

    Hi

    Ok so all is working and great scraped. is there a way to use query to switch between tweets and show like the latest 5 of something? if so how is this done?

    Thanks again anyone for the help

  • willy says:

    for me it doesn’t work when I try to get json

    var timeline = “http://marti1125.webfactional.com/lineauno/index.php”;

    $.getJSON(timeline, function(text){
    console.log(text);

    });

  • I love this code. Took me hours to eventually find something that works.

    Here’s what I have so far:
    http://www.whatnowebsite.co.uk/CustomCode/FacebookApps/WNW-TwitterFeed/Feed.php

    The intention is to feed this through to a Facebook Page Tab that I already have setup but for some reason when I put the following:

    <a href="url; ?>”>url; ?>

    It does not echo on screen.

    Any suggestions…?

    Also, is it possible to echo line breaks…?

  • zfa says:

    Thanks. U really helped my work a lot.

  • Red Knuckles says:

    Help! I followed your steps, but when I load get-tweets.php with Firefox or IE9, I only get a white page containing the following line:

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

    Yes, I made sure that the path to the twitteroauth library was correct. However, I’m loading the files off my hard drive. Do I NEED a web server for this to work?

  • Laura says:

    Okay I updated from PHP Version 4.4.9 to 5.3. It seems to be ok with line 17 now, but now it is giving me this error.

    Fatal error: Function name must be a string in /home/vg008web04/51/70/2907051/web/gettweets.php on line 19.

    Line 19 looks like this:
    $tweets = $connection(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”.$twitteruser.”&count=”.$notweets);

    Thanks!

  • Mary says:

    Hello. When I upload the php to my website all I see is “get(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”.$twitteruser.”&count=”.$notweets); echo json_encode($tweets);?>”

  • Laura says:

    Of course. Thank you Tom!

  • Tinus says:

    First of all thanks for the tutorial. I get the JSON parsed data in my seperate PHP file, but once I want to include it in my html I get an error in console that my URL isn’t allowed by the app settings. Which I think is weird because I get the data in the seperate PHP file, but not in the page which is in the same folder…

    Any clue whats going wrong here? I’m out of options.

    • Tom Elliott says:

      Hi Tinus, hmm – sounds like it could be a cross-domain issue which can occur if your accessing your feed from non-www and referring to the www in the twitter feed JS (or vice versa). If that’s not it, feel free to send me the link

  • Jeroen says:

    Tom, first of all. Thanks for this useful information. Appreciate the time and effort you make to get this thing going. At the moment i am trying this stuff out.

    – Downloaded that Abraham Zip file
    – Copy and pasted your code into the get-tweets.php
    – Also registered a new application and pasted the correct keys

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

    I did copy and paste the code 5 times now. It keeps saying. {“errors”:[{“message”:”Could not authenticate you”,”code”:32}]}

    What did i do wrong? Do you need more information. Just let me know mate.

    Cheers.

    Jeroen.

    • Tom Elliott says:

      Hi Jeroen, looks like you got the code 32 fixed when I checked your link.

      • Jeroen says:

        Tom,

        If you want to know what went wrong. While copy pasting the tokens into the PHP file it also copied a TAB as first character in the key. This was very hard to see. After i pasted the code from CODA 2 in Notepad++ I saw the problem. THe invisible TAB.

        So when you copy and paste the keys from your safari browser. Make sure to double check hidden “spaces” 🙂

        Greetings Jeroen

  • Owen says:

    HI Tom,

    Thank you so much for posting this – I’m sure I’m missing something really obvious, but I’m just getting this Parse error –

    Parse error: syntax error, unexpected ‘$twitteruser’ (T_VARIABLE) in /homepages/7/d347169079/htdocs/get-tweets.php on line 6

    link is – http://www.dontforgetdesign.com/get-tweets.php

    Any help you can give would be much appreciated.

    Thanks.

    Owen

    • Owen says:

      Sorry Tom,

      I was indeed being a little stupid.

      Sorted now.

      Classic case of spending too long staring at the screen and not being able to see the obvious!

      Owen

  • dwaves.de says:

    thanks man you helped me a lot!

    put together a working example ready 2 download.

    http://dwaver.wordpress.com/2013/06/25/twitter-api-migration-from-1-0-to-1-1-recent-06-2013-javascript-jquery-php/

    best regards!

  • Ross Gordon says:

    Hi there, have followed your instructions but I am not getting any tweets showing – if you could have a look at my site and assist me in any way it would be much appreciated!

    http://tworoxdesign.com/get-tweets1.1.php

    Thanks,
    Ross

  • DC says:

    Hi tom I think I found a small glitch in the relative_time func in some cases if you see the date of, examp …
    19 mar
    21 feb

    on your end via the script and then click over to twitter from your own site

    twitter has it listed as

    18 mar
    21 feb

    Ideas why this would be?
    and why this calc appears to be off with twitters?

    and a pos fix?

    Anyone else notice this?

    • Tom Elliott says:

      Hi DC – good spot, hadn’t seen this before! Perhaps it could be a locational time thing? My server’s in Germany for example whereas twitter.com I guess would be US based and the JavaScript relative time would depend on localisation

  • Bret Hawkins says:

    Hey Tom,

    I am a bit of a PHP/jQuery novice, and got thrown through a loop when the blogger.js twitter feed “plugin” got shut down. I am trying to go through this tutorial, but after I filled in the keys and username on my “get-tweets.php” it’s only displaying the last line of PHP code on that page.

    Any help you could provide would be awesome! Thanks.

    • Tom Elliott says:

      Hi Bret, the PHP isn’t being executed and doesn’t look like it’s enabled on your server. I’d speak to your hosts to see if your packages supports PHP

      • Bret Hawkins says:

        I realized the php wasn’t being executed because the copy/paste of your code above was missing a “?” before the first php tag… So now the php is being read correctly. However, if you look at the link I sent you, now it is simply returning a blank page. Any thoughts on why?

  • Alex says:

    Hey Tom,
    I tried setting up this authentication, downloaded the twitteroauth library and crated a get-tweets.php file with the correct keys input, and uploaded it to my personal site, but when I load the page, all I get is:

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

    output on the screen. No error or anything, just no json script showing up.

    Any ideas what’s wrong?

    • Tom Elliott says:

      Hi Alex, yes unfortunately it looks like PHP isn’t running on your server. If you view source, you can see the whole PHP which isn’t being executed.

  • Tom Elliott says:

    Hi Kev, the PHP file is fine but you’re using the old twitter JavaScript code and you need a script that will parse the tweets PHP. Check out my feed integration tutorial: https://tomelliott.com/javascript-ajax/custom-twitter-feed-integration-jquery/

  • Matthew says:

    Hi, sorry for my stupdity and thanks for trying to make simple what twitter changed from simple to complex for reasons i cant understand .. but ..
    SO this ISNT just a simple drop in and replace for the v1 that blogger.js was using?
    My tweets.php file seems to kick out the json fine for the 1 tweet I simply want to display but the blogger.js code isn’t dropping that in.
    Clearly im missing something and its simple. Any help please?
    Im just after my latest tweet and thats it. Thanks again.
    if this helps this is in my footer

    and this is the code to display the tweet

    function twitterCallback(obj) {
    var id = obj[0].user.id;
    document.getElementById(‘my_twitter_status’).innerHTML = obj[0].text;
    document.getElementById(‘my_twitter_status_time’).innerHTML = obj[0].created_at;
    }

    • Tom Elliott says:

      Hi Matthew, no – not a simple replace for blogger.js… it would require a lot of modifications to get the old blogger.js to work with new authentication scripts. If you haven’t checked it out already, my JQuery Twitter Feed tutorial should help. This essentially started out as a conversion from blogger.js

      • Matthew says:

        Hi Tim
        THanks for the reply, shame. Im not sure of Twitters reasoning for this but its unfortunate.
        I had a look at your Jquery page and it did give me a slight headache and its also somewhat overkill for the one tweet Im after without Twitters bells and whistles for their drop in solutions.
        Ill take a deep breath and try to work my way through and strip out what I dont need though.
        THanks again.

      • Matthew says:

        Hi Tim, sorted, thanks

  • gowthaman says:

    still its loading

  • Jen says:

    Thanks for this post, it was really helpful! …I’m just having one issue… I got the twitter feed working no problem on my test server, but when I duplicated it on my client’s server it no longer worked, so I checked the get-tweets.php file and it returned this error:

    Parse error: parse error, expecting `T_OLD_FUNCTION’ or `T_FUNCTION’ or `T_VAR’ or `’}” in /users/7/EC3/0CEC37/twitteroauth/twitteroauth.php on line 17

    I’m new to PHP so if anyone could help me decipher what it means, it would be greatly appreciated.

    Thanks!

    • Tom Elliott says:

      Hi Jen, that’s a new one to me – looks like it could be an issue with the version of PHP you’re using. The authentication library requires PHP 5.2.x

  • Wolvey says:

    Hi Tom,
    I got this to work on a test server (bluehost), but now that I’m trying to implement this on one of my work test sites it doesn’t work. I’m including the JS file in this work projects directory JS folder, but in that file, I’m using an absolute path back to the test server (//design.eqalhosting.com/Twitter/Test/get-tweets.php). I checked net tab and am getting no response. Any ideas?? I would like to send you the work test site url privately.
    Thanks

  • Liz says:

    I subbed in all of the required keys and tokens but I keep getting this error:

    {“errors”:[{“message”:”Timestamp out of bounds”,”code”:135}]}

    Do you know what’s wrong? The clock on my computer is correct.

    • Tom Elliott says:

      Hi Liz, first time I’ve seen this error code! Have you checked the clock on your server as well as your local machine? The following might help: [link outdated]

      • Liz says:

        Thanks for the link! Looks like it is a server issue as you said. Unfortunately, I’ll have to get through some red tape first. Thanks!

  • Keisa says:

    Hello Tom,
    I am trying my best to follow your tutorials as a beginner, but it’s extremely hard. I am not a programmer/developer so I am very confused. *sigh*

    Anyway, so I added this PHP//
    http://pastebin.com/YL3Kq2jU

    into a get-tweets1.1.php file.

    I uploaded “/twitteroauth” and visited “http://example.com/test/get-tweets1.1.php”

    but this error message popped up//
    “Fatal error: require_once() [function.require]: Failed opening required ‘twitteroauth/twitteroauth/twitteroauth.php’ (include_path=’.:/usr/local/php52/pear’) in /home/example/public_html/test/get-tweets1.1.php on line 3″

    Do you mind explaining what the issue is please?

    I’d appreciate it if you can help when you have spare time
    Please…

    • Tom Elliott says:

      Hi Keisa, it sounds like a path issue to the twitteroauth.php – you might just need to change your path to something more like require_once(“twitteroauth/twitteroauth.php”) but verify the file exists. Hope that helps 🙂

  • Mel says:

    Hi Tom,

    Sadly I’m one of the group getting “null” when I test. PHP version is new enough, cURL is installed and appears to play nicely with OpenSSL… any clues? I can email files across if you want, although all I’m actually using is the OAuth.php and twitteroauth.php libraries, plus your get-tweets example. The keys have been copied and pasted from the relevant Twitter dev page, I remembered to update the username… don’t think it can be a migration issue since I’m using your code! Any hints?

    Best,

    Mel

    • Mel says:

      (That said, making the get-tweets script pull from http://api.twitter.com rather than https:// does actually seem to work…. but so does cURL run from the command line and trying to pull over HTTPS. I wonder if PHP is looking at the wrong version of cURL or something.)