Stammy Script: RSS to Twitter using PHP
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)
The main source code for RSS to Twitter v0.1, based on this code.
- 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.