Post to twitter from your own custom pages

Are you a frequent twitter poster?
Here i am written a simple tutorial on how to post to twitter from your own custom page using twitter api.

As the twitter has changed its authentication method with OAuth, the below method will not work for you. I suggest you to read this twitter application with OAuth tutorial.

First we need to create a form to get the message that need to post to twitter.

This form will submit the message to tweet.php

Now create tweet.php and paste the below code,
Here the variable $data holds the message submitted from the form.

As you know, twitter does not accept more than 140 characters and so we need to limit the message to 140 characters.

Character limitation using php will use in automatic submissions like a blog post rather than limiting in the html form.

$twitter_data = "status=" . substr($data, 0, 136) ." ...";

Next we need to mention the login details of the twitter account to which the message to be tweeted. Change twitter username and password to yours.

$twitter_user = Username;
$twitter_password = password;

Here is the main code. Just add the below code as it is.

$twitter_api_url = "http://twitter.com/statuses/update.xml";
$ch = curl_init($twitter_api_url);
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}");
$twitter_data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Thats it.