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.




{ 94 comments… read them below or add one }
nice. I just had to comment on your use of “dude” in the error messages. :D
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.
@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.
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.
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.
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?
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.
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?
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.
Ryan – I meant if the URL you are trying to run through TinyURL has an & in it.
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!
Very cool! I DO seem to be getting some repeats, though… are you sure twitter protects against them?
http://twitter.com/dirtygreek
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))
My comments apparently got cut off, so just email me if you are interested. george at dirtygreek dot org.
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?
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.
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)
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
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?
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
@Damon, I’ll have to check that out. I haven’t messed with the script in quite a while due to other obligations.
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?
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?
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?
Thanks for the script. It worked like a charm for me @campascca
Appreciate you sharing this with all of us!
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)
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.
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.
This is great – and perfect for the site I’m building. Thanks!
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
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.
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
There are 2 files found. Which file should be ran?
when I try those files, I got 404 error :(
Please help me
There waz small problem with chomod settings.. I got it correct thanks :D
when I run it manually it works fine.. but when I try to cron job it it makes duplicate posts in twitter :(
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
Great script! Thanks a lot! Works perfectly “out-of-the-box”.
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’;
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!
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!
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.
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:
//$tiny_url = file_get_contents("http://tinyurl.com/api-create.php?url=" . $url);
//$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.
Thanks for the update Anthony! I’ll have to give that a whirl myself and update as necessary.
Any idea of how to handle special character such as quotes etc within the actually feed from stopping the post to twitter? Any time the actual content of my rss contains a quote, everything after the quote doesn’t get posted to twitter.. twitter.com/fantasy_nfl
thanks for any info you may have
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.
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:
$status = $title . " " . $tiny_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
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.
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!!!
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.
Hey Monica, have you made your code available anywhere yet?
I’d love to see it.
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
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.
Ado, let me know if you still need my code.
i would like the code please to post more than one item
I’d love that code – but I was also wondering if there was any way to space the items apart, to avoid a large clump of posts at one time.
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
Great Script – I have tried, twitterfeed, rss2twitter, rsstotwitter, and several others, they feed once and are done…This script will feed every time – if it is the last RSS in the list – that is fine with me!
2 Questions – The first you probably can help with, the 2nd??
1) I modified tw.php for the account @hamohio – it will send the latest updates from HamOhio.com – works great! (NO problems) – I may end up renaming the file to something I can remember thou! :-)
I also modified it to update @kd8bxp – and with that mod – I added to the status$ = “APRS Location: ” – the feed is my APRS location – I save the file under a new name and uploaded it to the server –
I only have parse.php on the server once – the question is – (After reading some of the post this made me wonder) – Do running the scripts from the same server but with two different feeds effect how parse.php works for duplicated posts? Both Feeds to the two accounts seem to be working correctly – so I don’t know if this is an issue or not.
The 2nd question I have – I tried and tried to get a cronjob to work – I had finally realized that the file permission needed to be changed – and I changed both tw.php and phrase.php to chmod 755 – after that the cron was able to run the script, but gave me errors (Lots of errors) -
” /home/swohcomp/public_html/tw.php: line 1: ?php: No such file or directory
/home/swohcomp/public_html/tw.php: line 2: /access-logs: is a directory
/home/swohcomp/public_html/tw.php: line 3: RSS: command not found
/home/swohcomp/public_html/tw.php: line 4: by: command not found
/home/swohcomp/public_html/tw.php: line 5: of: command not found
/home/swohcomp/public_html/tw.php: line 6: based: command not found
/home/swohcomp/public_html/tw.php: line 7: http://morethanseven.net/posts/posting-to-twitter-using-php: no such file or directory
/home/swohcomp/public_html/tw.php: line 8: access-logs/: is a directory
/home/swohcomp/public_html/tw.php: line 9: syntax error near unexpected token ‘parse.php’
/home/swohcomp/public_html/tw.php: line 9: include(‘parse.php’):
”
Any Ideas? Any Help? Please let me know – twitter.com/kd8bxp best way to get me!
Thanks – LeRoy
I got finally got crontab to work – It seems on the server I am using PHP was compiled as CGI – not Apache – as CGI cron didn’t like the script as written – I added
#!/usr/local/bin/php -q as the first line of the script, now it looks like a perl script and cron liked it – I also had to do a chmod to tw.php to 755 – Setup now and working!
BTW: The script still runs via the browser as well –
So for those who are having problems getting cron to work – it maybe that PHP was compiled as CGI – give it a try – it worked for me.
Thanks – LeRoy
OH Hey – One more question, just thought of it -> sorry about that –
Is there away to get the script to say “from HamOhio” (Or something else) instead of “from API” ?
Thanks again, great script! Thanks.
Hey dumdum, read Twitter API and helpdocs, that’s not possible unless you use OAuth, or have a Twitter app etc.
Does this work with google alerts?
Cant seem to satisfy the rss requirements keep getting the rss error. Dude dammit dude. why did i learn cfml my brain hurts. Any possible causes of rss problems – I tried pipes – my own – dont get it ?
Hi guys,
i´ve a problem. The script post the RSS-Entries somtimes double at twitter. How did I fix it? Thank you for answers.
I’m also getting the “Error: RSS file not found, dude.”
The script works when executing via a browser, but just returns the message when executed via a cron job.
I’ve tried with the normal site feed (a wordpress blog) as well as a Feedburner feed. I’m also doing the cron job on a Dreamhost account if it makes any difference..
Monica, will your script bypass the problem?
Any suggestions from anyone on how to solve this would be appreciated!
Thanks,
Mark
wget is too lame to handle the parentheses in the file name without quotes.
error
success
i got the error like this wht should i do plz reply me fast
Fatal error: Call to undefined function curl_init() in F:\websites\apache\twitter\tw.php on line 17
Hi, I have an url with & inside and I got an error when I click on the tiny url… Any idea how I remove this & and convert it in & ???
I have this when I click on the tiny url :
http://www.domaine.com/download.php?F=2561&type=bobox&rss=2
And I need this to work properly :
http://www.domaine.com/download.php?F=2561&type=bobox&rss=2
Arg … & amp; !!!
http://www.domaine.com/download.php?F=2561& amp;type=bobox& amp;rss=2
http://www.domaine.com/download.php?F=2561&type=bobox&rss=2
I have found a way to remove this :)
add this (ligne ~20) (I put a space between & and amp; you should remove it ;)
$url = str_replace(“& amp;”,”&”,$url);
under this
} else { die(‘Error: RSS file not found, dude.’); }
But How to send more than one feed a once ?? I have 10 news rss but only the last one is show … :’(
Hi there. @Paul Stamatiou, thanks for great plugin.
I would like to ask all of you : does anyone know how add ‘description’ or any other rss property like ‘comments’ or ‘author’ into parsing, right now only title and url are going through.
Thanks in advance
{ 101 trackbacks }