Post your tweet from twitter api and php
March 11, 2010 at 7:13 pm 2 comments
Twitter is the new social media sensational and many application developers want to add twitter with their site. Many times application developers want to send their updates,users comment and other information to twitter from their application. Twitter provides API to use its services.
Here I give you a very simple function that can post your tweet from your application to twitter. This uses cURL.
function postToTwitter($twitterusername,$twitterpassword,$tweet)
{
$host = “http://twitter.com/statuses/update.xml?status=”.urlencode(stripslashes(urldecode($tweet)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, “$twitterusername:$twitterpassword”);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == “200″)
{
$status=true;
}
else {
$status=false;
}
return $status;
}
This function posts your tweet successfully with the signature API on twitter.
Entry filed under: API, Uncategorized. Tags: API, curl, php, twitter, twitter API.
1.
CCTV Installation | March 3, 2011 at 12:24 am
Wow many thanks. This is very useful post. Can it be done in JSP as well as PHP? Which is easier?
2.
hau_pan | July 22, 2011 at 9:51 am
gghggggg