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:

  1. A cron job accesses the PHP script as often as you like.
  2. Utilizing lastRSS, the script grabs the latest post from the RSS feed you specify within the file.
  3. The script grabs the link to the article and uses the TinyURL API to create a shortened URL.
  4. PHP concatenates article title and tiny url to form a Twitter status message.
  5. 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.
  6. 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)

RSS to Twitter Source Code
The main source code for RSS to Twitter v0.1, based on this code.

Installation

  1. 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.
  2. Open tw.php and edit uname, pwd, feed with your Twitter login, password and the RSS feed you want the script to access, respectively.
  3. 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.

Download RSS to Twitter v0.1 (4 kB)