Update your Twitter status using PHP

Twitter is an awesome service! I use it on a daily basis and have grown addicted to it. Posting to twitter VIA an application is something that I'm sure you'll need to do at some point. Using this simple little script below, you can post updates to twitter accounts fairly easily. 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 Wink

18 comments for Update your Twitter status using PHP

Justin's picture

This works so well! You...

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

srdha's picture

i gust want to say some thing...

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

Ryan Gasparini's picture

$twitterUsername =...


$twitterUsername = trim($strPassword);

should be


$twitterPassword = trim($strPassword);

and you have this twice in the same if statement


!empty($twitterUsername)

Richard's picture

Hey Ryan, thanks for pointing...

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

Giyomu's picture

Hi ! I used your function for...

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 ?

++

Richard's picture

Hey thanks Giyomu, A link...

Hey thanks Giyomu,

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

Max's picture

How to add something to make...

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

Richard's picture

Max, not sure I know what you...

Max, not sure I know what you mean...

Max's picture

Your script is a part of my...

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?

Michael's picture

Max, the "time ago" output...

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!

Richard's picture

Thanks Michael!...

Thanks Michael!

MrMe's picture

I like it. Is there a way for...

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?

Richard's picture

MrMe, All you need is a form...

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.

Hector Pasch's picture

i really enjoy going through...

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.

Richie's picture

Hey and thanks for this great...

Hey and thanks for this great snippet.
Yesterday everything worked fine, but since today i cant post to twitter.
I always get "Could not post your Tweet to Twitter".

Does anyone know why?

man's picture

same here waiting for news...

same here
waiting for news

Mohammadreza's picture

I get 403 Error, Why?...

I get 403 Error, Why? Sad

man's picture

i got a 401 :/...

i got a 401 :/

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.