If you’ve used Twitter for long, you’re probably aware of their impressive API. Nearly any day-to-day task that you can perform on Twitter.com can be done programmatically via the API. (This enables us to have useful applications like Twhirl.)

Now, suppose you would like to enhance a website with some sort of automatic Twitter alerts. A blog automatically tweeting new posts is one obvious example.

The Twitter API Wiki contains all the documentation for the API. It’s best to read up on how it all works before you get started with too much API work. If you head over to the REST API page, the part we’re mainly interested is the statuses/update function. To make use of it, we need to send an HTTP POST request to http://twitter.com/statuses/update.format. The format part would be replaced with either xml or json, depending on the format we want the response to be in. Let’s go with XML.

The update function accepts two parameters in the request, status, which is the message to be posted, and the optional in_reply_to_status_id, which is used when the tweet to be posted is a reply to a user.

First, let’s prepare the necessary information and store it in variables.

$twitter_api_url = "http://twitter.com/statuses/update.xml";

$twitter_data = "status=Visit http://www.webmaster-source.com for PHP tips and tutorials!";

$twitter_user = "your_user_name";

$twitter_password = "your_password";

The $twitter_data variable contains the message to be posted to Twitter, which you must ensure is 140 characters or less before posting. You could use strlen() to do so when you ready the data to be sent, and truncate it if it’s too long. The status= before the message is the POST variable, the parameter for the API.

You’ll want to fill-in your Twitter username and password into the $twitter_user and $twitter_password variables, so the script can login and post to your account.

To send the request, we will use cURL, which is included on most servers. First we initiate cURL and pass it the URL we will be sending the request to:

$ch = curl_init($twitter_api_url);

Next we set the cURL options for the request. CURLOPT_POST to specify a POST request, CURLOPT_POSTFIELDS to set the data to be sent, CURLOPT_USERPWD to authenticate, and a few others that are necessary.

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $twitter_data);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_USERPWD, "{$twitter_user}:{$twitter_password}");

Next we execute the request, capture the HTTP response code, and close our cURL session.

$twitter_data = curl_exec($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

All that’s left really is to see whether it worked or not. Since we have our handy $httpcode variable, we can just run a conditional statement to see if the response code is equal to 200 (which means everything went okay).

if ($httpcode != 200) {

echo "<strong>Don't Panic!</strong> Something went wrong, and the tweet wasn't posted correctly.";

}

This is a fairly simplistic example. Obviously you would have to expand upon it to make it useful. You’ll need to prepare and pass data to it in some way, for one. If your messages will be accompanied by links, you’ll want to run them through a URL shortener first (Bit.ly has an API that you can use in a similar manner to Twitter’s to programmatically shorten URLs).

Update: This post is a bit out of date. For a more modern approach, that works with the changes Twitter has made to their API, see Post to Twitter From a PHP Script: 2013 Edition.