Twitter: kickball practice with @gomeler and the rest of my building

Stammy Script: RSS to Twitter using PHP

Jan 26, 2007 in , , ,

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)

Promote this article on various sites or email to your friends:     



90 Comments

  1. nice. I just had to comment on your use of “dude” in the error messages. :D

  2. 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.

  3. @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.

  4. 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.

  5. 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.

  6. 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?

  7. 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.

  8. 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?

  9. 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.

  10. Ryan - I meant if the URL you are trying to run through TinyURL has an & in it.

  11. 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!

  12. Very cool! I DO seem to be getting some repeats, though… are you sure twitter protects against them?

    http://twitter.com/dirtygreek

  13. 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))

  14. My comments apparently got cut off, so just email me if you are interested. george at dirtygreek dot org.

  15. 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?

  16. 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.

  17. 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!

  18. Cool script. One problem though - occasionally hiccups and sends the error message to Twitter, which is obviously not ideal. (ie [twittername]: Error)

  19. 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

  20. 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?

  21. 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

  22. @Damon, I’ll have to check that out. I haven’t messed with the script in quite a while due to other obligations.

  23. 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?

  24. 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?

  25. 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?

  26. Thanks for the script. It worked like a charm for me @campascca

    Appreciate you sharing this with all of us!

  27. 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)

  28. 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.

  29. Is there another choice for CHMOD? My CronJob seems to want something that allows executable (ie, 775)

  30. Nice script. I gonna use it on my blog to automaticcaly send new articles to twitter.

  31. This is great - and perfect for the site I’m building. Thanks!

  32. 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

  33. 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

  1. [...] Stammy Script: RSS to Twitter using PHP - PaulStamatiou.com Para no parar de twittear (tags: rss php twitter) [...]

  2. [...] I was playing around with the status service Twitter earlier today. I’ve known about it for a while, though hadn’t signed up until recently. I think it’s a pretty good idea, if not, heh, a bit stalker-friendly (though you can set your profile to only be viewable by friends). I think more than anything that the most interesting part is the many ways in which you could adapt the service to fit your needs, whether by just being creative (read: makeshift link/tumblelog–hey, that’s a good idea) or by utilizing software that makes use of Twitter’s API. One interesting use of Twitter that I’ve seen is syndicating your blog’s feed through the service, with each Twitter update being a new item in the feed. Also, Twitter’s made it very easy to update by providing two different ways to do so aside from the very intuitive web interface: via either IM or text message. [...]

  3. [...] might not have thought of it, but Paul Stamatiou’s latest script RSS2Twitter will be loved by internet marketing strategists, phony SEO specialists and many [...]

  4. [...] For now you have to use a hack, RSS to Twitter. [...]

  5. [...] was almost too easy: thanks to Paul’s RSS-to-Twitter script, PLAY.FM has now got a Twitter-channel announcing our latest [...]

  6. [...] Conectar un RSS con Twitter mediante PHP (inglés). Interesante script para, por ejemplo, publicar los últimos posts de tu blog en Twitter. [...]

  7. [...] RSS to Twitter. Php script by PaulStamatiou. [...]

  8. 73 Links That Rock Twitter! What are you doing?…

    Damn, I love Twitter! I’ve even declared it a MySpace killer. Why? Simply because of its addictiveness and simplicity. Who wants all the garbage and crap advertised on MySpace anyway? This is how social networking should be, not a high……

  9. [...] RSS to Twitter - Here is a PHP script to feed RSS to Twitter! [...]

  10. [...] has been getting some mixed reviews, but Paul Stamatiou has a hack that allows you to set your site’s feed (or any site’s feed for that matter) to show up [...]

  11. [...] hack: Site RSS to Twitter - Download Squad From Paul Stamatiou: The world has gone twitter. A new feature is integrating incoming RSS feeds to create a river of [...]

  12. Como enviar contenido a Twitter de forma automática desde tu web o blog…

    A estas alturas todo el mundo que esté al día de lo que pasa en la web sabrá que es Twitter.

  13. [...] options for feeding Twitter are RSS to Twitter and Paul Stamatiou’s PHP hack. To implement, just mash your feeds in Yahoo! Pipes, and then syndicate the result through the [...]

  14. [...] Stammy Script: RSS to Twitter using PHP - PaulStamatiou.com 把RSS输出到Twitter中的方法。 (tags: RSS twitter) Related: Diglog It « FlipFolio–桌面Widget软件 links for 2007-03-26 » [...]

  15. [...] RSS to Twitter - is a PHP script to feed RSS to Twitter. [...]

  16. [...] RSS to Twitter un script PHP para pasar el feed RSS a Twitter. [...]

  17. [...] RSS to Twitter - script en PHP para enviar tu feed a Twitter Via: Quick Online Tips Artículos [...]

  18. [...] RSS to Twitter - script en PHP para enviar tu feed a Twitter [...]

  19. [...] RSS to Twitter - 一個PHP的程式,將RSS收藏到Twitter中 [...]

  20. [...] RSS to Twitter - is a PHP script to feed RSS to Twitter. Share This | Leave A Comment (0) | Comments RSS | Posted 2nd May 2007 [...]

  21. [...] RSS to Twitter - is a PHP script to feed RSS to Twitter. [...]

  22. 《泡唠》创刊号暨五一长假特刊: 隐形的翅膀…

      脑子里有《泡唠》这个想法很长时间了,大约是从停止更新美味书签开始的,当时觉得没有一样互联网服务能够真正服务于自己的 blog,给 del.icio.us 也去过邮件,希望能改善 daily post çš„…

  23. [...] Stammy Script is a simple piece of PHP code which you upload to your server and will automatically convert RSS feeds to Twitter posts. Of course, TipMonkies has its own Twitter page making use of this little script (via Download Squad). [...]

  24. [...] you write a new blog post: twitterfeed, rss2twitter, PingTwitter, Twitter Updater (WordPress), or RSS to Twitter (PHP). Also check out Jaiku and iStalkr since they let you add feeds right out of the [...]

  25. [...] RSS to Twitter - is a PHP script to feed RSS to Twitter. [...]

  26. [...] Stammy Script: RSS to Twitter using PHP - PaulStamatiou.com RSS to Twitter using PHP (tags: php wordpress) [...]

  27. [...] RSS to Twitter - Um script PHP criado pelo conhecido Paul Stamatiu que cria RSS a partir das tuas entradas do Twitter. [...]

  28. [...] Conectar un RSS con Twitter mediante PHP (inglés). Interesante script para, por ejemplo, publicar los últimos posts de tu blog en Twitter. [...]

  29. [...] twitter Sharts - ‘Shart’ your twitter status anywhere within your wordpress blog posts or pages. rSS to Twitter - is a PHP script to feed RSS to Twitter. [...]

  30. [...] 8.Twitter Sharts   在文章中显示你的Twitter 状态插件。 9.RSS to Twitter 将RSS收藏到Twitter中 10.SimpleTwitter [...]

  31. [...] (11/14/07): So after looking over a few pages, and the inexplicable problems I had with the JSON api, I’ve updated the script [...]

  32. [...] RSS to Twitter - standalone скрипт на php, реализующий шлюз RSS2Twitter Posted in Web 2.0 | Leave a Comment [...]

  33. [...] Stammy Script é um código PHP que você deve fazer o upload para o seu server e ele converterá o seu RSS feed em posts no Twitter. [...]

  34. [...] Stammy Script: RSS to Twitter using PHP - PaulStamatiou.com (tags: twitter rss php code script mashup) [...]

  35. [...] RSS To Twitter - Como o próprio nome sugere, ele cria entradas de feeds (RSS) para suas mensagens no Twitter. É um script desenvolvido em PHP e roda em qualquer site/blog com hospedagem próprio, pois se faz necessário alterações nas permissões do arquivo. [...]

  36. [...] RSS to Twitter - es un script PHP para enviar tus feeds RSS a Twitter. [...]

  37. [...] RSS to Twitter - es un script PHP para enviar tus feeds RSS a Twitter. [...]

  38. [...] RSS to Twitter - script en PHP para enviar tu feed a [...]

  39. [...] RSS to Twitter - es un script PHP para enviar tus feeds RSS a Twitter. [...]

  40. [...] RSS to Twitter - script en PHP para enviar tu feed a Twitter [...]

  41. [...] RSS to Twitter. A excellent PHP script that feeds RSS to twitter. [...]

  42. [...] Stammy Script allows you to push your RSS feeds out across Twitter. I’ve seen a lot of people doing this, but I’m not sure whether I like it or not. If too many people start doing it then Twitter will die. [...]

  43. [...] RSS to Twitter - é um script para gerar um feed RSS para o Twitter. [...]

  44. [...] RSS to Twitter. Php script by PaulStamatiou. [...]

  45. [...] RSS to Twitter - is a PHP script to feed RSS to Twitter [...]

  46. [...] Shart  کردن تویتر در هرکجای وبلاگتان که دلتان خواست 10.RُُُُSSTo Twitter: کدی است به زبان PHP  که به کمک آن می توانید برای [...]

  47. Twitter Wordpress Plugins…

    Just spent the last hour finding all the Twitter Wordpress Plugins that are available…. did I miss any?

    Comment Twitter SMS NotificationGet notified by SMS when a user post a new comment on your blog.

    Twitter ToolsAutomatically posts to Twitter whe…

  48. [...] RSS to Twitter - script en PHP para enviar tu feed a [...]

  49. [...] method of embedding your Twitter status anywhere within your WordPress Blog posts or pages. RSS to Twitter PHP script to feed RSS to Twitter. TwitterComments Updates Twitter when a comment is posted on your [...]

  50. [...] RSS to Twitter -一个PHP的程式,将RSS收藏到Twitter中 [...]

  51. [...] RSS To Twitter - Como o próprio nome sugere, ele cria entradas de feeds (RSS) para suas mensagens no Twitter. É um script desenvolvido em PHP e roda em qualquer site/blog com hospedagem próprio, pois se faz necessário alterações nas permissões do arquivo. [...]

  52. [...] RSS to Twitter. Php script by PaulStamatiou. [...]

Post a comment, receive Stammy points.


Send a trackback.


  • If you plan on posting code, run it through Postable first.
Copyright © 2005 - 2008 PaulStamatiou.com  Privacy Policy - Terms of Service Can't spell my name? Use PSTAM.com. Go back up ↑.