PHP Programmer’s Of Bangladesh

Twitter: micro blogging site

Posted by: Md. Masud on: May 10, 2008

Twitter is one of the micro blogging site in the world. Actually people used Twitter answering a question “what are you doing?”. Twitter has done some great thing like included jaber interface to post directly from google talk or other IM who support jaber API. Twiter also provide a small API, by which you can post your msg from other site.

The API is looks like this which is collected from google code. Here is a simple view

function postToTwitter($username,$password,$message)
{

    $host = "http://twitter.com/statuses/update.xml?source=SITE_NAME&
             status=".urlencode(stripslashes(urldecode($message)));

    $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, "$username:$password");
    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"){
         $twitter_status='Your message has been sent! 
         <a href="http://twitter.com/'.$username.'">See your profile</a>';
    } else {
         $twitter_status="Error posting to Twitter. Retry";
    }
    return $twitter_status;
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }you just need a twitter account name & password and simply passed it to the function parameter and you will see the action in twitter. You can send the source name from where you send the message.

I have recently add this features to one of my application development and hereby i share this knowledge with all of you. good luck :)

NB: To see the code working you should activate the curl library from php.ini

Reference: twitter api is used from http://code.google.com

Leave a Reply

Where I was???

I was a PHP/MySQL web application developer in EVOKNOW INC from 1st July, 2006 to 5th Nov, 2007.

Blog Stats

  • 26,650 hits

What is the date?????

May 2008
S S M T W T F
« Nov    
 12
3456789
10111213141516
17181920212223
24252627282930
31  

My Links in Del.icio.us

Google Adds