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.
No related posts.


20 Comments until now
Awesome article. Simple and straight-forward. Great work.
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
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.
How did you get twitter to print out BrownPHP on the twitter page – I like that touch!
great Post!
works like a charm!
thanks!
Great post, but I could use some more info on how to set up the submission form…
Please give ur code that How did you get twitter to print out BrownPHP on the twitter page – I like that touch!
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.
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.
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
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.
Sure. I’ll take a look at it and let you know what I find.
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!’)));
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!
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.
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!
Tweetbacks
PHP Update: Use PHP to Twitter Your Tweets: Show the Twitter Love!
I recently got an email from a r.. http://tinyurl.com/8ycc34
Use PHP to Twitter Your Tweets http://bit.ly/Bv17
http://tinyurl.com/8ycc34 A simple #php script that posts tweets to #twitter
Trackbacks
Add your Comment!