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.


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.';
 } else {
 echo('Missing required information to submit your Tweet.');
 } else {
 echo('Curl Extension is not installed.');

Now call this method to send your tweet!

    // Send a Tweet to your Twitter account.

2 thoughts on “Update your Twitter status using PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s