Support Forums

Full Version: [CODE] Update Twitter using cURL and PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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($strMessage0140);
    }

    
//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($objCurlHandleCURLOPT_URL"$strTweetUrl");
        
curl_setopt($objCurlHandleCURLOPT_CONNECTTIMEOUT2);
        
curl_setopt($objCurlHandleCURLOPT_RETURNTRANSFER1);
        
//Tell cURL that we will be posting information
        
curl_setopt($objCurlHandleCURLOPT_POST1);
        
//Tell cURL to tell Twitter that our new status is $twitterStatus
        
curl_setopt($objCurlHandleCURLOPT_POSTFIELDS"status=$twitterStatus");
        
//Tell cURL that we need a username and password
        
curl_setopt($objCurlHandleCURLOPT_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!");
?>
This is awesome. Smile
Thank you.
Nice but how about a form too so you can submit the new tweet. Might even be cool to allow others to update your tweets like that.
Wow, that's really great. Thanks alot Jamza. Smile
Thank you very much Jamza for that update code in twitter. How’s the result of this API after you did the updates? I’ll open my twitter and will try this code. I’m looking forward for more update.


Try our free seo with seo indianapolis