I’ve been a Twitter user for a few weeks now and have come across many tools to help with Twitter posting and integration. I have also seen several Twitter mashups that essentially do RSS to Twitter and have become rather popular. At first it might sound stupid to use Twitter for syndication instead of tried and true RSS, but simply put, Twitter users are always aware of Twitter happenings, creating a more effective syndication experience. I like to call it micro-syndication as you only have 144 characters to get your point across.
After a bit of research the best way I could find to carry out RSS to Twitter involved a Jabber bot package for Ruby. Not too many people have access to a Ruby-enabled server so I decided to make a simple PHP script to get the job done. My code might not be the most efficient way of doing this, but here’s how it works:
- A cron job accesses the PHP script as often as you like.
- Utilizing lastRSS, the script grabs the latest post from the RSS feed you specify within the file.
- The script grabs the link to the article and uses the TinyURL API to create a shortened URL.
- PHP concatenates article title and tiny url to form a Twitter status message.
- Using the login and password you provided in the file, the script uses CURL to tap into the Twitter API and update the status message.
- The script doesn’t utilize RSS caching or any logic (which would have been nice) to determine whether the post is the same as the one currently on Twitter – but thankfully Twitter is smart enough to avoid posting duplicate status messages. That way, if your cron job calls the PHP script every 15 minutes and the feed hasn’t been updated in the last day, you won’t have a Twitter account full of the same status messages.
If you don’t know how to use or setup a cron job to execute the script, you can just run the script manually whenever your feed gets updated. For example, if you have a Twitter account just for your blog notifications, just hit up the script whenever you post a new article. However, if you want to make a standalone RSS to Twitter setup, you can try to grok this article on working with cron jobs.
Download RSS to Twitter v0.1 (4 kB)
Installation
- Download, unpack and upload to a public directory on your server. Ensure the files are at a CHMOD so that your Twitter login info is safe. CHMOD 644 should be fine.
- Open tw.php and edit $uname, $pwd, $feed with your Twitter login, password and the RSS feed you want the script to access, respectively.
- Save and test it out.
Comments
RSS to Twitter, as I’m calling it, is very basic and was the result of a spur of the moment coding session. LastRSS supports feed caching so in the future it’s very possible that I could develop a version smart enough to not attempt double posting, thus saving bandwidth (although it’s rather negligible). If you have any suggestions or code you’d like to see in this, please feel free to contact me and I’ll merge it in – consider it open source.

Tweet This
Stumble This


{ 92 trackbacks }
{ 74 comments… read them below or add one }
nice. I just had to comment on your use of “dude” in the error messages. :D
More from author
I’m testing this out right now and i get the following error when navigating to the tw.php file:
Fatal error: Call to undefined function: curl_init() in /home/www/filewarehouse.awardspace.com/twitter/tw.php on line 23
What version php did you get it to work for yourself? The host I am using has version 4.4.1.
More from author
@Ryan – you need to have PHP with CURL support. I believe I had the same version of PHP (4.4) you do. I’m looking into another way without CURL.
More from author
Just looked into curl support on the server I’m using and it says that it is enabled. It also give the following info on the curl support:
libcurl/7.13.2 OpenSSL/0.9.7e zlib/1.2.2 libidn/0.5.13
I’m using AwardSpace for the hosting needs.
More from author
Hrm, the error says it can’t find curl_init() which is part of any php greater than 4
http://us2.php.net/manual/en/function.curl-init.php
not sure what to say, but ill ask around.
More from author
I did a quick switch to another free host. I think the first one had disabled curl support, but never bothered to update their faq’s about it. Now i’m getting the following output from the tw.php file when browsing directly to it:
Error
success
Is that correct now?
More from author
I believe that error is coming from TinyURL – does the URL you are trying to use have a & in it? It currently chokes on that.
More from author
This is what I have:
$tiny_url = file_get_contents(”http://tinyurl.com/api-create.php?url=” . $url);
$status = $title . ” ” . $tiny_url;
echo $status; //just for status if you are directly viewing the script
$curl_handle = curl_init();
No “&” there. Any other thoughts?
More from author
There are no “&” in the entire file. I have the version that is available through the download link above. Any other possible thoughts? I’ll also test out another host.
More from author
Ryan – I meant if the URL you are trying to run through TinyURL has an & in it.
More from author
Thanks, Paul, for all your help. It turns out it was the site rss url that was the problem. It was all fixed up by “laundering” the rss feed through a yahoo pipe and using the pipe url in the tw.php file. It rocks!
More from author
Very cool! I DO seem to be getting some repeats, though… are you sure twitter protects against them?
http://twitter.com/dirtygreek
More from author
I’ve made a change to the parse.php script to use lastRSS’s cache feature. It checks the current rss feed against its cache and only attempts to post to twitter if there is a change. I THINK it’s working correctly.
in the tw.php script, you need to add
$rss->cache_dir = ‘./rsscache’;
$rss->cache_time = 3600; // one hour
but be sure to set the cache_dir variable to your own cache directory, and chmod that directory so that it’s writable from the script. Info on this is available on the lastrss website.
In parse.php, I basically replaced
if ($timedif cache_time) {
// cached file is fresh enough, return cached array
$result = unserialize(join(”, file($cache_file)));
// set ‘cached’ to 1 only if cached file is correct
if ($result) $result['cached'] = 1;
}
with
if ($timedif cache_time) {
// cached file is fresh enough, return cached array
$result = unserialize(join(”, file($cache_file)));
$result2 = $this->Parse($rss_url);
if(count(array_diff($result,$result2))
More from author
My comments apparently got cut off, so just email me if you are interested. george at dirtygreek dot org.
More from author
Hi, great work! I can’t get it to work though. I keep getting the RSS file not found message. I have tried different feeds and none seem to work. Ideas on what I am doing wrong?
More from author
I’ve done some more fun stuff using (at least partially) Paul’s code and methods, but for some reason I had to use patRSS instead of lastRSS. Anyway,
http://www.dirtygreek.org/journal/journalId/2042
What this script does is read a Google Calendar feed and check each event’s time against the current time. If the event’s time falls within a given span around the current time (default is one hour), it will post the event’s details to Twitter. In other words, Google Calendar will tell Twitter what you’re doing at the current time.
More from author
I keep getting the “Error: RSS file not found, dude.” error, even though the RSS file does exist – I’m linking directly to it, and have checked it by going directly to the rss file in a browser and getting the expected xml page. Any ideas why I’d be getting this error?
Thanks in advance for any help!
Cool script. One problem though – occasionally hiccups and sends the error message to Twitter, which is obviously not ideal. (ie [twittername]: Error)
More from author
Nice script, thx a lot! It works just fine on my integration of Twitter and Plazes: http://dlinsin.blogspot.com/2007/06/twittering-with-plazes.html
Regards, David
More from author
Looks like something changed on Twitter. After 2 months – suddenly at midnight I have been getting duplicate posts of the latest status message every time the script runs. Any thoughts?
More from author
Hey guys,
I implemented a simple check to prevent multiple updates, check it out if your are interested:
http://dlinsin.blogspot.com/2007/06/twittering-with-plazes-fix.html
More from author
@Damon, I’ll have to check that out. I haven’t messed with the script in quite a while due to other obligations.
More from author
It worked for a few days, but with nothing changes I only get ‘Disallowed Key characters’ as output. Can anyone give a hint what’s going wrong?
More from author
The script seems to have some problems: it doesn’t cut the username at the beginnig of the message and it doesn’t update the twits regularly.
Have you got some ideas in particularly for the second problem?
More from author
I’ve tried this script and I’ve noticed that Twitter doesn’t check for duplicated posts like you have said.
Is it need to activate any option?
More from author
Thanks for the script. It worked like a charm for me @campascca
Appreciate you sharing this with all of us!
More from author
Dreamhost (and others) doesn’t allow @fopen with external urls – I’ve modified parse.php to use curl instead to get the rss file – email me if you’d like a copy.
(I will also let LastRS known and share the modifications back to them if they want them)
More from author
hello,
i tried this on my blog, but it wont send notification to twitter unless i go to http://www.myblog.com/tw.php then it tells me success and i receive the message.
this is the normal thing to do? or is there any problem?
thank you.
More from author
Is there another choice for CHMOD? My CronJob seems to want something that allows executable (ie, 775)
Nice script. I gonna use it on my blog to automaticcaly send new articles to twitter.
More from author
This is great – and perfect for the site I’m building. Thanks!
More from author
Hello Paul, there is a small and easy-to-use PHP library for sending messages to Twitter and receiving status updates – http://phpfashion.com/twitter-for-php
Thanks for the link Jeff – Unfortunately it relies upon php5/simplexml which not many people have on their servers. I love simplexml though, much easier to work with http://paulstamatiou.com/2007/04/17/how-to-parse-xml-with-php5
More from author
How can I add a comment to the script the puts “Most Current Podcast” at the start of the twitter message.
Thanks for any help
I was just wandering how to change the “from web” tag at the end of the twit
I would love it to say from MyWebsiteName.com
great for SEO
Great script! How do I get it to add more than one twit though? Is that all it adds the first time around or will it eventually add more?
I heard that the guys who created jobfeedr.com are releasing their RSS to twitter technology for public release in the next few weeks, so I guess when that comes out you might be able to get muiltiple feeds into the one twitter account Jeromy.
More from author
Thanks for this script, Paul! I modified it to keep track of my World of Warcraft server status. https://twitter.com/ravenholdt
Thanks for the script, I have put my weather station onto Twitter! @budawx
More from author
There are 2 files found. Which file should be ran?
when I try those files, I got 404 error :(
Please help me
More from author
There waz small problem with chomod settings.. I got it correct thanks :D
More from author
when I run it manually it works fine.. but when I try to cron job it it makes duplicate posts in twitter :(
More from author
I found your script which is great, however despite apostrophes showing correctly in the feed, they’re showing as \’ on Twitter. I think this is an escape character issue, but wouldn’t know where to start to correct it. Any clues?
Thanks,
Emma
Having problems with Twitterfeed…but this script worked like a charm. Now I’m in control of how often my site’s RSS is posted to my Twitter!! Thank you so much–this is exactly what I have been searching for over the past few days!!!!
–Paul
More from author
Great script! Thanks a lot! Works perfectly “out-of-the-box”.
More from author
The script fuctions withour crashing but all I get when I executive it manually is the title of the first item of the RSS feed on my browser window under php, but nothing gets posted to twitter
is this a curl problem or something else?
Does it automatically cut the title to fit the url and still be within 140 characters?
Hi there,
The script works fine , no duplicates (alth0oug have run it manually so far, no cron set up yet)…
My problem is that the output is displayed as follows in twitter.
http://tinyurl.com/xxxx
Also , is there a way to display more that 1 feed at a time. i.e. to run it every hour and display 3-4 feeds at a time. More or less like in twitterfeed but without having to rely on their service and availability! :-)
Any help greatly appreciated. :-)
P.S. Ty for the script
Doesnt come up right in this form! :-)
Basically CDATA is literally send to twitter
….CDATA… http://tinyurl.com/
To mousia,
Try: var $CDATA = ‘content’;
More from author
Works like a charm :-) Its actually very handy script.
Σ’ ευχαριστω !
Hello Paul,
I was wandering if you would consider updating the script for a PHP 5 and up context. Maybe even with some extra ‘parsing’ and output optimization tutorial/option. If not, would you consider consulting on it? (You can email me for that)
Thanks!
More from author
Great script! 2 questions:
1. How can I import more than just the last rss post (meaning more than 1)
2. Why does it come out in this format? (What is the CDATA etc.) Can I just see the title?
Thanks!
More from author
Hi Yuval,
Firstly I am not a coder, I just find my way around! Probably there is the “correct” way and obviously you are not a developer either if you asking that:-)
1. Yes you can. Check out pw.php and find line 18-19. You can change that so it feeds any number you want from the list of feeds! i.e. change 0 to both lines to 4 or whatever you want, to feed rss feed number 4 from the rss feeds list etc. You could also consider duplicating, or making as many copies as you want of pw.php(renamed of course), varying the numbers (and/or the rss path if you have rss feeds for multiple categories),hence setting up as many cron jobs as your pw.php(renamed) files are (so for each one of them). If you more skilled then you can just code some sort of a loop there also.
2. Try: var $CDATA = ‘content’;
This is in parse.php line 40.
Again I am not a developer, but this will do the magic.
Sorry for my english :)
Anyone know how to successfully use Bit.ly with authentication with this? I tried but it don’t work (bad coding).
And how can I check to stop dupe posts to stop API uses per hour all being used?
Did anyone ever get this script working with bit.ly instead of tinyURL?
Also, RSS’s with apostrophes in don’t work. Do you know how to fix?
Try to escape it. http://www.godaddy.com/test\’s/
Not sure if it will work but it would in C++ =)
thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.
More from author
Jake/Adam: I just ran into this code today and I’m super excited about it. I modified the script to use bit.ly instead of tinyURL.
You’ll have to supply your own bit.ly login and API. I’m also handling the special characters in the titles with urlencode($title).
Below, I left the commented out tinyURL reference in the script so everyone will know where I placed my change.
Here’s my bit.ly code:
//$status = $title . ” ” . $tiny_url;
$bit_lySource = file_get_contents(”http://api.bit.ly/shorten?version=2.0.1&longUrl=”.$url.”&login=YOUR_BIT.LY_LOGIN_NAME&apiKey=YOUR_BIT.LY_APIKEY&history=1″);
preg_match(’@”(shortUrl.*)”(.*?)”@i’,$bit_lySource, $matches);
$bit_ly = $matches[2];
$status = urlencode($title) . ” ” . $bit_ly;
I’m looking into changing the duplicates issue that I’m seeing with the code. I’ve also wrapped this into a function and created a loop so that more than one RSS feed is read and posted.
When I get the duplication resolved, I’ll post some more code. I haven’t written PHP in a long time, so expect some less-than-spectacular, but working results.
More from author
Thanks for the update Anthony! I’ll have to give that a whirl myself and update as necessary.
More from author
Anthony, that’s great, thanks so much.
I’ve tried it out, however I’m getting the following error when I execute tw.php
Parse error: syntax error, unexpected ‘:’ in /MY_PATH/tw.php on line 22
My line 22 has…
$bit_lySource = file_get_contents(”http://api.bit.ly/shorten?version=2.0.1&longUrl=”.$url.”&login=YOUR_BIT.LY_LOGIN_NAME&apiKey=YOUR_BIT.LY_APIKEY&history=1″);
*with my bit.ly login name and API key changed of course
Adam, that’s odd… my code doesn’t have a colon in it at all… I’ve placed a text file of this here: http://drop.io/akv_rsstotwitter/
The file is called tw_snippet.txt and it’s the same thing as what I posted. Maybe that source file will get you better results.
More from author
Hi everyone. Sorry if this a dumb question, but I’m new to this sort of coding.
I have the script running, and it does a good job. However, the RSS feed I want to put on Twitter often uses titles that are longer than the amount of characters allowed by Twitter. How can I get the program to grab only the first, say, 120 characters of the title (allowing room for the URL at the end)?
Thanks in advance.
Monica:
Here’s a php snippet to shorten the tweet to 140 characters and an ellipsis symbol (…) between the shortened title and the url:
if (strlen($status)>140) {$status = substr($title,0,136-strlen($tiny_url)) . “… ” . $tiny_url; }
I’ve also posted this update to a drop that I’ve created for these changes: http://drop.io/akv_rsstotwitter
More from author
Thanks for your help with shortening the URLs!
Has anyone been able to modify this to use it with Atom feeds? Seems like LastRSS will not process them, or at least the one I’m trying to use.
Thanks the script full job.
More from author
I still don’t get it… How to post all 10 items from feed to twitter? What ever i try to change it gives me errors, and unchanged version tweet only 1 latest item from feed even with var $items_limit = 10; Any help PLEASE!!!
More from author
Any Ideas?
Ado,
I’ve rewritten the script to use SimplePie instead of LastRSS (to solve my Atom feed problem) and the one I’ve written will also post multiple items. I don’t have the code with me at home, but when I return to work I’ll post it. But if you spend some time using SimplePie, you could probably figure it out.
well i can try it (probably) easy with magpie as i use it frequently but i thought someone had solved this problem within this code… Thanks anyway.
More from author
Monica can you please send me your code, i couldn’t make it working right with magpie as it making problems with special characters :s
More from author
WOW… Finally i fixed current code to post all rss items to twitter :))) If anyone is interested to have it, please let me know here!!! I will just test it next few hours.
More from author
Hi folks,
I have a problem with the script, it outputs this: Error: RSS file not found, dude.
I set up a feedburner feed, but it doesnt work… any ideas?
thx a lot