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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?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.'); } } ?> |
Now call this method to send your tweet!
1 2 3 4 |
<?php // Send a Tweet to your Twitter account. tweetThis('my_user_name', 'my_password', 'my_tweet_message'); ?> |
should be
and you have this twice in the same if statement
This works so well! You should make this a plugin! Thanks so much!
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
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 ?
++
How to add something to make “from EXAMPLE” appear below the tweets?
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?
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.
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?
I get 403 Error, Why? 🙁