11-28-2009, 08:22 AM
By now, we have all probably used or at least heard of Twitter by now and also seen the number of websites and programs which integrate with twitter, but how do they do it?
They all use something called the Twitter API (Application Programming Interface). This allows all sorts of applications to interact with the services that twitter offers.
I have created a simple function that will update your twitter status and will walk you through it in comments.
You can now use this function like this:
They all use something called the Twitter API (Application Programming Interface). This allows all sorts of applications to interact with the services that twitter offers.
I have created a simple function that will update your twitter status and will walk you through it in comments.
PHP Code:
<?php
function twitterstatus($strUsername = "", $strPassword = "", $strMessage = "") {
//First off, we check to see whether cURL is installed and we can use it
if(function_exists("curl_init")){
//Lets just make our Username and Password more useable
$twitterUsername = trim($strUsername);
$twitterPassword = trim($strPassword);
//Twitter only allows messages of 140 character, so lets check if our message is less
//than 140 characters and if it is no, shorten our message
if(strlen($strMessage) > 140){
$strMessage = substr($strMessage, 0, 140);
}
//Remove any nasty inputs from our message
$twitterStatus = htmlentities(trim(strip_tags($strMessage)));
//Just check to see if we have everything thats required, the username, password and message.
if(!empty($twitterUsername) && !empty($twitterPassword) && !empty($twitterStatus)){
//This is the Twitter API URL for updating our status message
$strTweetUrl = "http://www.twitter.com/statuses/update.xml";
//Initialise cURL Handle
$objCurlHandle = curl_init();
//Tell cURL the URL of the Twitter API
curl_setopt($objCurlHandle, CURLOPT_URL, "$strTweetUrl");
curl_setopt($objCurlHandle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($objCurlHandle, CURLOPT_RETURNTRANSFER, 1);
//Tell cURL that we will be posting information
curl_setopt($objCurlHandle, CURLOPT_POST, 1);
//Tell cURL to tell Twitter that our new status is $twitterStatus
curl_setopt($objCurlHandle, CURLOPT_POSTFIELDS, "status=$twitterStatus");
//Tell cURL that we need a username and password
curl_setopt($objCurlHandle, CURLOPT_USERPWD, "$twitterUsername:$twitterPassword");
//Tell cURL to Execute our request
$result = curl_exec($objCurlHandle);
//Get cURL to split our reply into a handy array
$arrResult = curl_getinfo($objCurlHandle);
//Now lets check if we were successful at updating out status
//Twitter will reply 200 if everything went ok
if($arrResult["http_code"] == 200){
echo "Your Tweet has been posted";
} else {
echo "Could not post your Tweet to Twitter.";
}
//Close out cURL Handle
curl_close($objCurlHandle);
} else {
echo('Missing required information to submit your Tweet.');
}
} else {
echo('Curl Extension is not installed.');
}
}
?>
You can now use this function like this:
PHP Code:
<?php
twitterstatus("myusername", "mypassword", "my new twitter status!");
?>