in Webdesign
in php

 Post to twitter from your own custom pages

12345 (average: 5.00 out of 5)
Loading ... Loading ...

Share |

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 first we need to create a form to get the message that need to post to twitter.

<form id="tweet" name="tweet" method="post" action="tweet.php">
<textarea name="tweet-msg" rows="5" cols="50"></textarea><br />
<input name="submit" type="submit" value="Tweet it" />
</form>

This form will submit the message to tweet.php

Now create tweet.php and paste the below code,

<?php $data = $_POST['tweet-msg']; ?>

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.

Download source


Write your comment