Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

Update your Twitter status using PHP

Twitter is awesome! Using the simple script below you, you can post updates to twitter. Don’t forget to add your username, password and message below.

<?php
function tweetThis($strUsername = '', $strPassword = '', $strMessage = '') {
    if (function_exists('curl_init')) {
        $twitterUsername = trim($strUsername);
        $twitterPassword = trim($strPassword);
	if(strlen($strMessage) > 140) {
	    $strMessage = substr($strMessage, 0, 140);
	}
	$twitterStatus = htmlentities(trim(strip_tags($strMessage)));
	if (!empty($twitterUsername) && !empty($twitterPassword) && !empty($twitterStatus)) {
	    $strTweetUrl = 'http://www.twitter.com/statuses/update.xml';
	    $objCurlHandle = curl_init();
	    curl_setopt($objCurlHandle, CURLOPT_URL, "$strTweetUrl");
	    curl_setopt($objCurlHandle, CURLOPT_CONNECTTIMEOUT, 2);
	    curl_setopt($objCurlHandle, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($objCurlHandle, CURLOPT_POST, 1);
	    curl_setopt($objCurlHandle, CURLOPT_POSTFIELDS, "status=$twitterStatus");
	    curl_setopt($objCurlHandle, CURLOPT_USERPWD, "$twitterUsername:$twitterPassword");
            $result = curl_exec($objCurlHandle);
            $arrResult = curl_getinfo($objCurlHandle);
            if ($arrResult['http_code'] == 200) {
                echo 'Your Tweet has been posted';
            }
            else {
                echo 'Could not post your Tweet to Twitter.';
            }
	    curl_close($objCurlHandle);
	}
	else {
	    echo('Missing required information to submit your Tweet.');
	}
    }
    else {
	echo('Curl Extension is not installed.');
    }
}
?>
<?php
	// Send a Tweet to your Twitter account.
	tweetThis('my_user_name', 'my_password', 'my_tweet_message');
?>

Follow me on Twitter ;)

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

13 Responses to “Update your Twitter status using PHP”

  1. Comment #1 - Permalink
    Justin
    05/15/2009

    This works so well! You should make this a plugin! Thanks so much!

  2. Comment #2 - Permalink
    srdha
    05/20/2009

    i gust want to say some thing “great job”

    Update your Twitter randomly according to your intrest Or, from Rss Feed Or, from your own tweet message list Or, Any combination of the above three http://feedmytwitter.com

  3. Comment #3 - Permalink
    Ryan Gasparini
    05/20/2009

    $twitterUsername = trim($strPassword);

    should be

    $twitterPassword = trim($strPassword);

    and you have this twice in the same if statement

    !empty($twitterUsername)

  4. Comment #4 - Permalink
    Richard
    05/20/2009

    Hey Ryan, thanks for pointing that out. :) I updated the post.

  5. Comment #5 - Permalink
    Giyomu
    06/30/2009

    Hi !

    I used your function for my website, it is very useful ! But, how I refer to you on my acknowledgments page ? I should display a license or something else, make a link to this page or to your site ?

    ++

  6. Comment #6 - Permalink
    Richard
    06/30/2009

    Hey thanks Giyomu,

    A link back would be appreciated but not required. Enjoy!

  7. Comment #7 - Permalink
    Max
    10/14/2009

    How to add something to make “from EXAMPLE” appear below the tweets?

  8. Comment #8 - Permalink
    Richard
    10/14/2009

    @Max, not sure I know what you mean…

  9. Comment #9 - Permalink
    Max
    10/15/2009

    Your script is a part of my website. If a user posts a tweet using a form that’s connected to this script, below the actual Tweet’s content, you see in grey “1 minute ago from API”. How to get there “1 minute ago from myWEBSITE”?

    I already have a Consumer Secret and a Consumer Key to create a Twitter application. According to the API Wiki of Twitter, this is needed to let there appear “from myWEBSITE”. How to use them in your script?

  10. Comment #10 - Permalink
    MrMe
    12/09/2009

    I like it.

    Is there a way for a user to add there username and password and message, rather than have it in the code?

  11. Comment #11 - Permalink
    Richard
    12/12/2009

    @MrMe,

    All you need is a form to capture their credentials and pass it to the function VIA the posted data. Make sure to sanitize the input.

  12. Comment #12 - Permalink
    Michael
    05/04/2010

    Max, the “time ago” output that you’re thinking about is directed from Twitter after you call on a message using the Javascript API. If you don’t like it, try using the JSON or XML methods. This function is so that you can write a Tweet from your own server.

    Thank you Richard!

  13. Comment #13 - Permalink
    Hector Pasch
    06/12/2010

    i really enjoy going through your posts here and i’ve been watching from a distance but felt the need to comment to let you know. keep it up. is there a RSS feed? I just installed bloglines and want to add it in there.

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top