Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[CODE] Update Twitter using cURL and PHP
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:
function twitterstatus($strUsername ""$strPassword ""$strMessage "") {
//First off, we check to see whether cURL is installed and we can use it
//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 "";
//Initialise cURL Handle
$objCurlHandle curl_init();
//Tell cURL the URL of the Twitter API
//Tell cURL that we will be posting information
//Tell cURL to tell Twitter that our new status is $twitterStatus
//Tell cURL that we need a username and password
//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){
"Your Tweet has been posted";
            } else {
"Could not post your Tweet to Twitter.";

//Close out cURL Handle
    } else {
'Missing required information to submit your Tweet.');
    } else {
'Curl Extension is not installed.');

You can now use this function like this:
PHP Code:
("myusername""mypassword""my new twitter status!");
LockerZ Invite Required? PM Me.
Visit TechBeat for the latest Technology News
[Image: jamzasigniture.jpg]
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.
Superman I am here to rescue you.
This is Support Forums not Support PMs.  Do not PM me for support unless it's private and site related.
Wow, that's really great. Thanks alot Jamza. Smile
At the top will be the same place you hang from.
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

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 847 07-27-2020, 11:26 PM
Last Post: tk-hassan
  IMDB rating using cURL and PHP sarvsav 1 1,659 04-15-2014, 04:43 PM
Last Post: Callum
  My PHP Project, V.03 [UPDATE] ChromeWolf 3 1,027 11-17-2011, 12:59 PM
Last Post: HF~Legend
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,400 10-10-2011, 01:00 PM
Last Post: Greyersting
  How to write good php code [Must Read] BannedPoop 19 6,116 08-30-2011, 08:28 AM
Last Post: Slash

Forum Jump:

Users browsing this thread: 1 Guest(s)