Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[CODE] Update Twitter using cURL and PHP
#1
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!");
?>
LockerZ Invite Required? PM Me.
Visit TechBeat for the latest Technology News
[Image: jamzasigniture.jpg]
Reply


Messages In This Thread
[CODE] Update Twitter using cURL and PHP - by Jamza - 11-28-2009, 08:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 936 07-27-2020, 11:26 PM
Last Post: tk-hassan
  IMDB rating using cURL and PHP sarvsav 1 1,780 04-15-2014, 04:43 PM
Last Post: Callum
  My PHP Project, V.03 [UPDATE] ChromeWolf 3 1,138 11-17-2011, 12:59 PM
Last Post: HF~Legend
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,660 10-10-2011, 01:00 PM
Last Post: Greyersting
  How to write good php code [Must Read] BannedPoop 19 6,469 08-30-2011, 08:28 AM
Last Post: Slash

Forum Jump:


Users browsing this thread: 4 Guest(s)