Posting from an RSS feed to twitter using OAuth
Being the lackadaisical webmaster that I am, I hadn’t noticed any of twitter’s recent announcements that all scripts using api access to twitter would need to authenticate via OAuth instead of basic password authentication.
I had been using a php script to take stories from my RSS feed, shorten then with the bit.ly URL shortener and post them to twitter. The script stored the twitter account username and password and was logging in and posting the tweets.
As per twitter’s OAuth announcement, at some point on September 1st 2010 the script stopped posting tweets from my RSS feed.
This tutorial will show you the script I use to post tweets from RSS and how I got the script working again with OAuth.
The original script I was using was created by Paul Stamatiou and I had made a few modifications. The new version uses some OAuth stuff from Tayeb and the twitteroauth library from abraham on github.
The first thing we need to do is login in to twitter and create a new application at https://twitter.com/apps/new
Give the application a logical name and fill in the other fields. Application Type should be Browser, Default Access Type should give Read & Write permissions and you should check the Use Twitter For Login box.
Once you submit the form you will be taking a page that shows your Consumer and Secret Keys and some twitter authentication URLs.
You will be needing the Consumer and Secret Keys so keep the tab open for later.
Next you will need to grab the twitteroauth library from http://github.com/abraham/twitteroauth
You can grab it easily from git on Linux by doing :
git clone http://github.com/abraham/twitteroauth.git
from the command line.
We’re going to be using the twitteroauth library to create oauth tokens for our app.
So, create a new folder and upload the files to your server. Let’s say we upload all of the files under mysite.com/twitteroauth
We need to edit the file config.php to include our keys from twitter and our callback URL.
So open up config.php and go back to that twitter.com application details page and use the keys to fill in config.php like below. You will also need to specify the callback URL as callback.php in the folder where you uploaded the twitteroauth files.
define('CONSUMER_KEY', 'YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET', 'YOUR_CONSUMER_SECRET_KEY');
define('OAUTH_CALLBACK', 'http://www.mysite.com/twitteroauth/callback.php');
Next you will need to goto mysite.com/twitteroauth/connect.php
Load that page in your browser, click the Sign in with Twitter button and follow the instructions to give your application access to the account you want to post your RSS items to.
Once that’s done goto http://dev.twitter.com/apps, click the name of your app and then click ‘My Access Token’ from the bar on the right.
This page gives us the remaining 2 oauth tokens we will need to authenticate the script.
At this point we only need a couple of files from the twitteroauth library and don’t need the whole twitteroauth folder to be on our server any more as we were just using it to authenticate ourselves.
Create another new folder on your server, lets call this one tweets, so it should be something like mysite.com/tweets/
This folder will be permanent and contain the scripts to automatically post our RSS items to twitter.
In the twitteroauth folder that we got from git there is another folder inside called twitteroauth (so the full path is twitteroauth/twitteroauth). This folder should contain 2 files, OAuth.php and twitteroauth.php.
Upload this folder with the 2 files to mysite.com/tweets/twitteroauth/
In this new folder at mysite.com/tweets/twitteroauth/ create a new file called secrets.php and put the following inside secrets.php :
$consumer_key = "YOUR_CONSUMER_KEY"; $consumer_secret = "YOUR_CONSUMER_SECRET_KEY"; $access_key = "YOUR_OAUTH_TOKEN"; $access_secret = "YOUR_OAUTH_TOKEN_SECRET";
(make sure to wrap the above in php open and close tags, I had to remove them because they were confusing wordpress)
The next thing we need to do is add 2 files in the mysite.com/tweets/ folder; parse.php and index.php
index.php is the file we will call to send our tweets, and it calls parse.php to get the items from our RSS feed.
You can get parse.php from the original .zip file from Paul Stamatiou and then you can either use the index.php from Tayeb or use my modified version below.
My version makes a couple of modifications; it uses the bit.ly URL shortener instead of tinyurl; it replaces ampersand characters in titles (&) with the html entity, and it automatically shortens the title/tweet and appends an elipsis if the tweet and bit.ly URL are going to be too long for a tweet.
If you want to use my version, you will need an account at bit.ly and your bit.ly API key from http://bit.ly/a/account
Here’s my modified index.php file.
You will need to replace ‘YOURBITLYLOGIN’ with your bit.ly username and ‘YOURBITLYAPIKEY’ with your bit.ly API key. Also replace ‘YOUR RSS URL’ with, well…the URL of your RSS feed.
If you have done everything right up to this point, when you go to mysite.com/tweets/index.php it should pull the last few items from your RSS feed and post them to twitter.
If you want this to be automatic (which you probably do!) then just set up a cron job to call the mysite.com/tweets/index.php script periodically.
Edit 3rd September 2010 :
Rob Q in the comments asked if it was possible to have a version of index.php which posted only titles from RSS to twitter, while ignoring the links. If you want to do this too, this version of index.php should do the trick.
Thanks for posting this. Found myself with this problem myself and needed an oath script that was already created.
Do you know if there is any easy way in you index.php script to disable the use of adding a bitly (or tiny URL)link? I’m already creating a twitter ready link when I create a feed post in the title and so just need to send that feed title to twitter and not add a bitly link to it.
I receive the following message when authorizing my twitter acct to use the script (it does it but I get the error message) and then a second time when I’m trying to send a tweet with index.php. Any ideas what I can do to remedy?
Fatal error: Call to undefined function json_decode() in /www/xxxxxx/public_html/tweets/twitteroauth/twitteroauth.php on line 137
Reply
I have the same problem as Rob Q mentioned on the post above, could not find a solution yet, help us if you can please. Btw thanks for the informative post, appreciated!
Rob – Are you using PHP 5.2.0 or higher? If not, you might have to run an upgrade, or install the JSON extension. If you *are* running 5.2.0 or more recent, make sure you did not compile php with –disable-json.
JSON is basically a lightweight version of XML.Here’s some info from the PHP website on the JSON extension:
http://www.php.net/manual/en/json.installation.php
Rob – I think Peter has already answered your error message question very well, it is probably true that you are using a version of PHP less than 5.2.X or don’t have the JSON extension installed.
For your other question (posting without links) it should be a simple matter of removing the parts of the script that handle the link. Here’s an index.php with those parts removed, although I haven’t tested it – can you let me know if it works?
I’ll add it to the bottom of the article too.
http://www.mabujo.com/assets/tweets/index_no_url_php.zip
Thanks so much for your excellent documentation. You Saved Me!
And, for Rob Q., in trying different oauth samples, I found that php 5.2.9 was causing me errors so I upgraded to 5.3.3. Even though I was using oauth code different than what is found here, I had errors associated with how my php extensions were installed. And these were extensions that have been working just fine in all the other code I’ve written in the last two years! If you use windows, you can find 5.3.3 here, http://windows.php.net/download/.
And I offer what I have found to be the bare minimum of code for index.php:
http://pulpnews.com/oauth_index_example.zip
“Could not connect to Twitter. Refresh the page or try again later.”
when typing http://www.mysite.com / twitteroauth / connect.php Show me the text above
I did all the received CONSUMER_KEY, CONSUMER_SECRET, access_key and access_secret
boki – that error message from twitteroauth/connect.php is because you have not set up the constants in twitteroauth/config.php
it’s the stuff mentioned in the second code box in the tutorial…
Thanks for the replies. I’m on a server that doesn’t support json and I’ve tried a couple ways to add that ability but I’m not getting there – just doesn’t seem to work. I did find a basic oath script and am able to send a tweet out using it despite receiving the json error code after it loads. Of course, this does not check a feed or page to import text to send so I’m not farther along.
boki, I received that error too. It was because I had a typo in the script.
Hello,
I have installed and while browsing the index.php page it shows me the latest RSS but it doesn’t show the updates on twitter page… any ideas???
Thanks,
I installed everything but error starts from first time
shows
“Could not connect to Twitter. Refresh the page or try again later.”
so I MUST pay at least 7 per month to make this work? Not complaining…but is this correct?
thx
[...] my last post on posting tweets from an RSS feed with OAuth, I have been spending a few fun hours having a play around with the various Twitter APIs and have [...]
“Could not connect to Twitter. Refresh the page or try again later.”
Could this be temporary?
I got it to work, but it just tweets the title and the short url. Is there any way to include part of the description too?
I added this to the index.php:
$description = $rs[items][$newpost][description];
And then I put:
$status = $title . ” ” . $description. ” ” .$tiny_url;
But that didn’t work.
And a cache to check for duplicates would be nice also. I only want to tweet one post at a time(if my feed had 20 new items), like every couple of hours instead of all at once.
Thanks
“Could not connect to Twitter. Refresh the page or try again later.”
I’m also getting this and I don’t have any typos
Don’t works with my account. It connect to Twitter but don’t post anything.
Thanks for the tutorial
the code works fine for me. But, I have one question, how to add another rss feeds at index.php so we have 2 RSS. Thanks before
I got it to get descriptions also, and cut it to less than 140 characters. Change and add the lines in the index.php to the ones these:
if ($rs = $rss->get($feed)){
$title = $rs[items][$newpost][title];
$description = $rs[items][$newpost][description];
$url = $rs[items][$newpost][link];
} else { die(‘Error: RSS file not found, dude.’); }
$tiny_url = file_get_contents(“http://tinyurl.com/api-create.php?url=” . $url);
$status = $title. ” ” .$description. ” ” .$tiny_url;
if (strlen($status)>140) {
$status = $title. ” ” .substr($description,0,136-strlen($tiny_url)-strlen($title)) . “& #8230;” . ” ” .$tiny_url; }
echo $status;
……………………………..
Take the space out between the & and #82830. that makes an eclispe…
I would still like it to drip feed my posts to twitter slowly instead of all at once. Anyone have any ideas???
Looks promising. I have a daily photo feed that I’d like to share via a new Twitter account I’m starting. Will have to try out this script over the weekend.
“Could not connect to Twitter. Refresh the page or try again later.” Could this be temporary?
“Could not connect to Twitter. Refresh the page or try again later.” I’m also getting this and I don’t have any typos
My rss feed gives me such items:
http://www.whatsoever.com/abc/bdef/6481/The_Article_Martha_/d4ever/
The Article Martha
Description text, … blah blah blah
I try to call the index.php directly in the browser and get nothing till it says, it wants to download index.php. This index.php is empty.
With other rss feeds I get results.
Do you have a hint?
Hello, can i use this linkshortener 88c.defor clickbank ?. when i use it, the link shows only the 88c.de/withsomenumber. how can i use it with link cloaking ?
many many thx my friends.
I can’t get hold of PARSE.PHP as Paul has removed the script..anyway of getting hold of it?
Found parse.php here
http://www.daniweb.com/web-development/php/threads/257694
If you are using bit.ly you must encode the URL if it contains any special characters otherwise you will get an error from bit.ly.
The response from bit.ly will use a decoded URL as the index to the shortend URL.
$url_enc = urlencode($url);
$bitly_call = file_get_contents(“http://api.bit.ly/shorten?version=2.0.1&longUrl=” . $url_enc . “&login=raphotoclub&apiKey=R_df7240b39d32d5b5e2eba46f219970c1&history=1″);
$bitlyinfo=json_decode(utf8_encode($bitly_call),true);
$tiny_url= $bitlyinfo['results'][$url]['shortUrl'];
regards
jre
Is there any way to add a cache to this so you don’t get double postings ?
You shouldn’t need too, twitter automatically filters out duplicate consecutive postings?