Posting to Twitter using PHP

Like others I’ve found myself becoming something of a fan of Twitter, the impossible to explain social networking site. If you’re reading this, have a twitter account and not already my friend then add me if you like.

Apart from the interesting social aspects I’m also interested in Twitter as an API for all sorts of communications, remember Twitter already deals nicely with SMS messaging, Instant Messaging, subscription and the like and has a nice XML and JSON based API. I’ve been using the Zamano SMS gateway at work for a few projects and Twitter actually lets me some more and doesn’t come with a price tag.

I started out playing with curl to send updates like so (obviously with a real username and password):

curl -u username:password -d status="twittering from curl" http://twitter.com/statuses/update.xml

I then used the PHP curl features to do the same thing from PHP:

<?php
// Set username and password
$username = 'username';
$password = 'password';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
}
?>

Obviously you could do more with the return than print out a success or failure message. The $buffer variable has the returned XML or JSON for you’re parsing pleasure.

I’m going to try out some of the other API methods too, probably play more with XSL or look more closely at the PEAR JSON module in building up a simple library as a quick search didn’t throw up much of interest and the API is nice and simple; making it fun to hack on.

Comments

  1. Thanks for the nice code. Very quick and useful. I’ve used it to fix a bit of code I discovered which Twitters whenever I post a new Wordpress blog entry. More details here: http://james.cridland.net/code/twitterupdate.html

    James Cridland - 4th March 2007

  2. Ah ha. I tried that wordpress plugin and it didn’t work for me either but didn’t feel enough pain to actually work out if it was just me or to fix it. Nice work. I might have to give it another go.

    gareth - 4th March 2007

  3. Wow, thank you very much for this! I was feeling somewhat lazy on a Sunday afternoon but was also somewhat interested in writing a php script which would send a random joke up to twitter that I could hide away instead of dedicating a cron job to it. Thank you very much sir!

    Christopher Kinniburgh - 18th March 2007

  4. [...] Escribir en Twitter utilizando PHP (inglés) [...]

    Twitter: recursos para una idea simple y feliz | 5líneas.com | Internet, desarrollo web, tecnología y otras cosas por descubrir - 19th March 2007

  5. [...] I still didn’t want to manage two different “status” accounts, and I thought: “Maybe someone wrote a script that lets you use your Facebook status to update Twitter.” After a bit of searching, I couldn’t find anything… so I decided to try writing it myself. I was lucky enough to find articles by Dave Dash on using Facebook to update Adium, and Gareth Rushgrove on posting to Twitter using PHP. Many thanks to both of them for making their code available! [...]

    Twitterbook: Update Twitter using Facebook Status | Design Meme - 22nd March 2007

  6. Thanks for making this available! It was very helpful in creating Twitterbook—a script to get the status from your Facebook account to update your Twitter.com status as well. You can download it from here: http://www.designmeme.com/2007/03/22/twitterbook/

    Stuart Robertson - 22nd March 2007

  7. [...] Morethanseven » Posting to Twitter using PHP Posting to Twitter using PHP (tags: php twitter) [...]

    links for 2007-06-10 &laquo; Attractive Verdana - 10th June 2007

  8. [...] It’s very simple but it does what I need it to do. It’s based on an idea that Gareth Rushgrove writes about in an article on posting to twitter using php. If you find this useful, have any suggestions or want to modify or improve this code feel free but please let me know by commenting on this post and I’ll merge it in. Consider it an open source. [...]

    David Currin > Blog Archive > Use Twitter to notify you when you have new Gmail - 22nd June 2007

  9. [...] Escribir en Twitter utilizando PHP (inglés) [...]

    GRATIS EN INTERNET > Twitter: recursos para una idea simple y feliz - 24th August 2007

  10. Works like a charm – Thanks very much. Modified it just a touch for my use -
    on success: header(location: $_SERVER[HTTP_REFERRER]);
    The form submits and goes right back to the original page.
    Very cool, thanks again!

    Eric Danielson - 1st December 2007

  11. [...] Readings Tutorial on using Timeline About JSON Posting to Twitter using PHP and CURL twitter api Google’s method of dynamic json data import json inspector Creating timeline from [...]

    Timeless Belt Of Time: How I Integrated Twitter in Timeline at kepo-ing Zz85 - 29th December 2007

  12. [...] The main source code for RSS to Twitter v0.1, based on this code. [...]

    Stammy Script: RSS to Twitter using PHP - zakeh blog - 15th January 2008

  13. Great script.
    There is just one thing that has been bugging me: using special characters, specialy á, é, ã, and so on. If I send the string with these chars in them, the message printed on twitter only goes up to that char and stops. I’ve tried urlencode and htmlentities and so far nothing.
    Any ideas?
    Cheers and gratz on the script!

    Sean - 12th March 2008

  14. thank you for this. I was about to make one, but luckily I googled first :)

    fedmich - 2nd May 2008

  15. [...] Ensuite, il vous faut récupérer le script qu’a écrit Gareth Rushgrove, disponible ici: [...]

    Un script pour twitter avec PHP (et Curl) &#8212; Etan Online - 3rd May 2008

  16. Really nice article and script but I have the same problem using special characters in turkish alphabet.

    araba - 6th May 2008

  17. [...] to Gareth Rushgrove’s detailed explanation of how to post to twitter with php, I was able to turn an extra 20 minutes downtime into something (potentially) [...]

    > A flippant use of an API :: Joshua Blount is a web developer, designer, and all around good guy in Lake County, Florida :: - 12th May 2008

  18. Thanks, I just started to learn twitter api. this gives me a really good start!

    Ben - 14th May 2008

  19. [...] Morethanseven > Posting to Twitter using PHP [...]

    antiprocess.net > Blog Archive > Linkit: 19.5. - 22.5. - 22nd May 2008

  20. Cheers, used this idea to write a wrapper script for Linux to Tweet from the commandline or launcher. http://www.guru.net.nz/blog/2008/06/tweeting-in-linux.html

    GuruBob - 8th June 2008

  21. [...] Get a good script. I used this one from a guy called Gareth Rushgrove, as it’s php+curl and works [...]

    Twitter API - How to send tweets from a Wordpress &#8220;Tweet this&#8221; button - 17th June 2008

  22. Thanks alot, i used this script to help kick start QuickTwit.com,
    you can download the source aswell if you want to see what ive done with it.

    Adam Wilkinson - 19th June 2008

  23. Hello Gareth, 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

    Jeff Coel - 3rd July 2008

Comments are now closed.