Updating Twitter status with PHP

Have you ever wanted to know what’s your friend doing right now? But you don’t want to constantly call him or her just to find out what’s going on. Ask your friends to join Twitter community for free and you’ll be able to follow their tracks watching their updates online. While sitting comfortably in your chair you may also tell your friend what you are doing. Send an update over the web or using your favorite chatting service and let them know. In case you are out of office and not near the computer or having access to the Internet, use your mobile phone and send an instant short message to update your status in the community. Well, that’s easy, isn’t it!

You will find Twitter a fun way to tell the world what on the Earth you are doing, but you might toss a question in the air: "Can I update my status while writing my blog?" The answer is simple and short: Yes you can! Whether you are using popular blog software like WordPress or administering your content manager yourself it’s relatively easy to integrate Twitter update in the underlying software. For WordPress you’ll find numerous plugins to spice up your installation and for those who are running their own software I have implemented a simple code snippet with PHP to show how to do the trick. I’ve chosen PHP, as it’s the de facto of Internet computing.

Implementing Remote Update

First you’ll need a free Twitter account that you can sign up instantly. Go for it and join the community right away! You will then supply your registration with a password of your choice. Remember to choose strong password, as this will be sent over the net to update your status. Your registration email address will be your username when sending the update. Twitter limits the status message to 140 characters, so you’ll need to truncate messages too long. In case your message contains letters common in many European languages, wrap it with utf8_encode() function. The following script uses curl library, so you must enable this before attaching the code in your application. Ask your server administrator to enable curl library functions in case you are not familiar with backstage administration. You can check the current status of you server installation using phpinfo() function. It tells you whether you have curl enabled or not.

$usr = "enter your email address here"; $pwd = "enter your password here"; $msg = "enter your new status message here"; $value = "status=" . utf8_encode($msg); $url = "http://twitter.com/statuses/update.xml"; $session = curl_init(); curl_setopt($session, CURLOPT_URL, $url); curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); curl_setopt($session, CURLOPT_POST, 1); curl_setopt($session, CURLOPT_POSTFIELDS, $value); curl_setopt($session, CURLOPT_USERPWD, "$usr:$pwd"); $buffer = curl_exec($session); curl_close($session);

For successful updates Twitter returns an XML file in $buffer variable to indicate the new status in the community. You may then examine the XML and refresh your local status display accordingly. This sample code uses XML for updates, but you can, of course, use JSON as well in case you’re programming with Ajax, for example.

Final Words

With some extra programming you can easily expand the possibilities of this little helper. You might send an update to Twitter community each time you hit the save button to publish your article in your blog. Or, if you are editing an existing one, the code will tell the others that the article has changed. Finally, if you deleted something, let people know about it as well. Just add some testing before calling the code above to check if you are creating a whole new article or just updating an existing one.

Julkaistu maanantaina 5.3.2007 klo 16:55 avainsanoilla harrastukset ja Internet.

Edellinen
Virtuaaliminä
Seuraava
Picasan verkkoalbumit