Show the Twitter Love!

I recently got an email from a reader asking me if I could help them with a small script to post updates to a Twitter account using PHP. I figured this might be something that a few others might be interested in, so I decided to post about it.
As most of you know, there are a multitude of options when deciding how to to Tweet to Twitter. If you would like to add this functionality to your site so that you don’t have to download an application to your computer, or visit another site, this might be something you would like.
Using the simple script below you, you can post updates to twitter. Please BE ADVISED: this script needs altered to run. As well as some extra code to add your desired functionality.
$username = 'myUserName';
$password = 'myPassword';
$status = urlencode(stripslashes(urldecode('This is a new Tweet!')));
if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);
if ($resultArray['http_code'] == 200)
echo 'Tweet Posted';
else
echo 'Could not post Tweet to Twitter right now. Try again later.';
curl_close($curl);
}
Needed Alterations:
- Change $username to YOUR username
- Change $password to YOUR password
- Edit $status to the Tweet that you want posted.
Code to Add:
- Some HTML to Tweet dynamically. For example: Use a form like below and have $_POST['new_tweet'] update the $status variable.
- More Filtering: It would probably be a good idea to add some more filtering to the status so that you can’t just post a blanks status etc. (I could add that easily, but then where would be your opportunity to learn!)
- Use OOP Practices: Creating a Twitter Class out of the below procedural code would make it much more modular for you in the future. (Again, better for you to do this yourself. Feel free to contact me with any questions.)
- Add Some Javascript: Adding some Javascript that counts the amount of characters in the Tweet field would be handy to have for your visitors.
Try it out for yourself (Your Twitter Username and Password are NOT being recorded):
Tweet from BrownPHP if you like to test it out for yourself. You can also just copy this code, and make the necessary changes. I just wanted to give a small example to give you an idea of what you can do. Let me know what you think about it and if you have any ideas for some code scripts that you would like to see.
If you enjoy using Twitter, you may want to take a look at our Twitter Tag Cloud Service that we provide. You can also download the scripts to run the script from your own site.
Addendum (05/12/10):
This code has been added to the BrownPHP Code Repository if you would like to download it. You can go straight to the download by clicking the BrownPHP Twitter Class
January 13th, 2009 on 1:55 pm
Awesome article. Simple and straight-forward. Great work.
January 14th, 2009 on 10:37 am
If its of any interest, there is a Twitter component in ZF as of 1.7.
http://framework.zend.com/manual/en/zend.service.twitter.html
Tweet in 2 lines.
In either case, Good post!
Ralph
January 31st, 2009 on 4:14 pm
Hi there,
I am completely new to php and was wondering if you wouldn’t mind giving me an insight into what the script looks like which handles the submitted form so that tweets can be added dynamically.
thanks.
January 31st, 2009 on 6:39 pm
How did you get twitter to print out BrownPHP on the twitter page – I like that touch!
February 1st, 2009 on 6:35 am
great Post!
works like a charm!
thanks!
February 22nd, 2009 on 5:10 am
Great post, but I could use some more info on how to set up the submission form…
March 5th, 2009 on 1:19 am
Please give ur code that How did you get twitter to print out BrownPHP on the twitter page – I like that touch!
March 17th, 2009 on 7:21 am
I provided the code that I used in the article. As for getting Twitter to put BrownPHP (or your sitename), I think I did it by filling out this form on Twitter.
http://twitter.com/help/request_source
It was a little while ago, so my memory could be a little fuzzy, but I know you can’t do it through coding. You have to have Twitter accept the request to have your link added to the Tweets.
March 29th, 2009 on 6:41 pm
This is great code! I was stumped for a little bit since I am new to PHP and such. First thing that confused me was how to get the information from the form to the external php. Not too hard, action=”external.php”. Then how did the external PHP get the form info (i.e. input type=”text” name=”username” to $username). PHP code is: $username = $_POST['username']; Last thing with my method is that I kept getting errors due to the echo command in the external PHP file. It required double quotes instead of single quotes.
Thanks so much for posting this code! I’ve learned something new, again… Now to conquer the world, one tweet at a time.
May 11th, 2010 on 2:24 pm
Thanks for the examples here and for this reply of details for those like myself who are new to php. The questons you had were mine as well so I’m very glad to found this page:)
May 11th, 2010 on 7:59 pm
Well, I'm glad this post was able to help. Keep an eye on the blog in the next couple months, I'm going to be adding this code to the BrownPHP Code Repository.
Also if you enjoy coding in PHP join the BrownPHP facebook fan page on the right side of the screen, or follow the BrownPHP Twitter!
May 9th, 2009 on 1:50 am
I can’t send a message with @xxx hello. There is a problem with @ character. It works on your test but not with your script.
May you help me? Thanks
March 13th, 2010 on 5:53 am
Add this to the code:
$status = str_replace(“%40″, “@”, $status);
May 10th, 2009 on 10:35 pm
Thanks for the script. Works great! However, if my status update contains the ampersand (&) character, the message gets posted only until the & character. Any ideas on how to overcome that? I guess these characters need to be escaped or url encoded or something.
May 11th, 2009 on 5:15 am
Sure. I’ll take a look at it and let you know what I find.
May 11th, 2009 on 11:07 am
Ok, I took a look at it and I updated the script. It looks like it is just a urlencode issue.
So change:
$status = ‘This is a new Tweet!’
TO:
$status = urlencode(stripslashes(urldecode(‘This is a new Tweet!’)));
May 14th, 2009 on 6:53 pm
Thanks for a nice, clean and simple example.
I am integrating Twitter updates into an existing application and tried several classes and APIs that were just not working. So you officially win the KISS award!
May 16th, 2009 on 6:00 am
Thanks, I wanted the post to be simple enough that anyone trying to get started with it can use it no matter your skill level.
May 21st, 2009 on 1:45 pm
Great little app for sending Tweets .. but is there an app that does the REVERSE. I.e. takes the tweets of the people you are following and re-posts them onto a website (NOT TWITTER ITSELF – That is now BLOCKED by big brother here)that you can access? I don’t mean like a frame that pulls in twitter content, I mean something that goes to your twitter account and parses through your friends’ tweets and spits them out onto a third-party page of your choosing. I ask because I USED to use it FOR work from the office, but they blocked it – and I don’t have time or energy to search for tutorials etc before or after work… hence looking for a workaround. If you know of any .. THANKS!
Also something that’s a reverse bit.ly encoder would be very cool too
MAJOR WISHLIST ITEMS
Thanks for any ideas and this great little bit of code!
July 9th, 2009 on 11:27 pm
Thanks for this! I’ve implemented it on a client site. Unfortunately it looks like Twitter is not accepting new “sent from” entries for non-OAuth sources, so tweets from this script come from “API”. Which certainly could be worse.
September 4th, 2009 on 8:16 am
Nath – Check out the below link. I already commented on where you can go to have the Tweets show where they came from on Twitter.
http://www.brownphp.com/2009/01/use-php-to-twitter-your-tweets/#comment-2224
September 24th, 2009 on 8:10 am
The easy way to get a proper ‘from’ source is no longer available to new scripts. Seems you will have to suffer the OAuth disaster.
October 3rd, 2009 on 1:58 am
Brilliant Post! Thanks for this I’ve finally got my custom lifestream page post to Twitter.
October 27th, 2009 on 4:23 pm
works great, took me a while to work out the changed because im a noob but im sure this will help me greatly in an upcoming project
February 11th, 2010 on 12:53 pm
can someone please print out the code because it is not working for me
February 26th, 2010 on 12:11 pm
Sir…..Its very nice to have this code but…….how can we check whether the entered User name and password is correct or not………….Kindly help me
March 6th, 2010 on 10:32 pm
Is it possible to link to your website in a tweet?
May 12th, 2010 on 8:39 pm
Sure just add to the $status variable…
$status = $status . 'http://www.brownphp.com';
I would rather use a short URL service here and use that though. If you have a long url, that could hurt your 140 character length.
March 10th, 2010 on 4:17 am
I have used it.It is working fine for me.
Thanks
Thirupathi.J
April 9th, 2010 on 10:03 am
Hi,
nice script. Unfortunately it doesn´t work for me.
I debugged a little and found out, that after curl_init() nothing happens anymore.
Is this a problem of my webserver or any settings in the php.ini are wrong?
Cheers
Marcus
May 12th, 2010 on 8:34 pm
Wow, sorry for the long delay in the response. Short answer, it appears that your curl extension is commented out in your php.ini file.
Check you php.ini file out and look for: extension=php_curl.dll
Remove the semicolon out if one exists.
July 25th, 2010 on 12:32 am
Thank you for the simple tutorial and code.
Btw your download link is broken please check it.
July 25th, 2010 on 3:43 am
Its not working…
I have tried your above form also but its not working.
Always displaying error “Could not post Tweet to Twitter right now. Try again later.”
Please update the script.
Thank you.