Some days ago I decided to experiment with Twitter API. They offer a really simple to use API and good documentation that allowed me to create a function to submit to twitter using PHP and the curl library.
So, Twitter offer in its documentation something like this…. ” Post a status update and get the resulting status back as JSON: curl -u username:password -d status=”your message here” http://twitter.com/statuses/update.json “
With this in mind we will be building our twitter_it() function.
Please remember to make the necessary validations and checks. This is just a simple example to demonstrate the the usage of the Twitter API.
<?php
/**
* This function will send a new status to your Twitter account
* Usage: twitter_it(“message goes here”);
*
* @param string $new_status
*/
function twitter_it($new_status = NULL) {
//TWITTER API URL
$twitter_url = “http://twitter.com/statuses/update.json”;
//Initiate curl
$curlh = curl_init();
//Post fields in a array
$data = array (“status” => $new_status);
//Set curl options.
curl_setopt($curlh, CURLOPT_URL, $twitter_url);
curl_setopt($curlh, CURLOPT_USERPWD, “<user>:<pass>”);
curl_setopt($curlh, CURLOPT_POST, 1);
curl_setopt($curlh, CURLOPT_POSTFIELDS, $data);
//Excute curl with options
curl_exec($curlh);
//Close connection.
curl_close($curlh);
}
?>
If there is other way to do it. I’ll be glad to know and maybe enhance this one.
1 Comment for Twitter it
Edwin Huertas | %A %B %e%q, %Y at %I:%M %p


Hi Edwin it’s the ‘other’ Edwin. I just came across your post on Google and thought I’m chime in with my experiment using Twitter’s API. I’ve got it all just about completed, but my problem is their connection. Do you experience bad timeouts constantly? BTW: The site I’m working on is called twittors.com