Twit with PHP[updated]
- December 12th, 2008
- Posted in PHP
- Write comment
While my brain cells are doomed to extinction in writing my pangram, I’ve written a simple tutorial on how to send a twitter updates using PHP. And I think this will be good while I’m waiting for my brain cells to regenerates.
This tutorial requires CURL library installed for PHP, it’s use for communicating with the Twitter API. Here is the demo of the script.
Read at the bottom for updates.
So, let’s begin:
First we declare the following variables.
1 2 3 4 5 | $user = 'username'; $pass = 'password'; $twitter = 'http://twitter.com/statuses/update.xml'; $status = 'Set your Status here'; $status = trim(urlencode(stripslashes($status))); |
The variable $user and $pass are of course your username and password in your twitter account. The $twitter is the URL of the updates for the Twitter API with the status parameter, $status is where the status/update will be store.
Then, let’s do the curl. We will create a curl session.
6 | $curl = curl_init(); |
Set the necessary options for our session. This is where our variables will be needed.
7 8 9 10 11 12 | curl_setopt($curl, CURLOPT_USERPWD, $user.':'.$pass); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_URL, $twitter); curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:")); |
The first option, the CURLOPT_USERPWD is where you define your username and password in a format of [username]:[password] to use for our request. Next the CURLOPT_POST, we need to define this as true because the request must be a POST. Then the CURLOPT_URL, is where the URL to do our request. And lastly the CURLOPT_RETURNTRANSFER, it returns the value of our curl execution(later) as a string instead of printing it directly to the browser.
We execute and catch the status of our request.
11 12 13 14 15 16 17 18 19 20 21 22 | curl_exec ($curl); $HttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); switch($HttpCode): case 200: echo 'Your status has been updated'; break; case 401: echo 'Not Authorized: invalid username or password.'; break; default: echo 'An unknown error has occured. Try Again'; endswitch; |
Here, after we executed the curl, we would like to get the status of our last request with the function curl_getinfo() with the option CURLINFO_HTTP_CODE. This will let us know what our last HTTP code, since the Twitter API returns the appropriate HTTP Status code for every request.
Lastly, we close our session and frees all resources.
23 | curl_close($curl); |
And that’s all.
The complete code:
Here is the complete code that are describe above. You can use/modify in any way you want.
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 | $user = 'username'; $pass = 'password'; $twitter = 'http://twitter.com/statuses/update.xml'; $status = 'Set your Status here'; $status = trim(urlencode(stripslashes($status))); $curl = curl_init(); curl_setopt($curl, CURLOPT_USERPWD, $user.':'.$pass); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_URL, $twitter); curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:")); curl_exec ($curl); $HttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); switch($HttpCode): case 200: echo 'Your status has been updated'; break; case 401: echo 'Not Authorized: invalid username or password.'; break; default: echo 'An unknown error has occured. Try Again'; endswitch; curl_close($curl); |
Updates for HTTP/1.1 clients
I’ve check twitter API and there are a lot of changes ever since I write this post, so here are the updates:
Since it is a post operation, we’ve set
10 | curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status); |
And for the HTTP/1.1 clients thats getting an Http response of 100, we’ve overwrite the expectation to empty in our header when sending our request. Link
13 | curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:")); |
Comment out, if your’re having problem.

Facebook
Linkedin
Virb
Flickr
Deviantart
Last FM
Delicious
Roboto
Tumblr
Twitter
Gtalk
Aim
Skype
Email

THANK YOU!!!!
I have uploaded the demo to my server at
http://vedwatches.com/tweeterAPI/twitter_php/twitter.php
but its not working. Whats wrong ?
Curl is also enabled, you can check the phpinfo at
-removed-
Please reply soon.
I updated the files. Thanks.
Can you please post the script for the demo?
Hello. The script of the demo is the same as in the downloadable archive.
Hi Rogelio,
I love this code. I have been trying for hours to make this work on my site but can’t for whatever reason.
Any ideas?
Here is the link:
http://americantrip.org/twitter.php
I have php on my server and have Libcurl 7.10.3. But is that the correct one?
-Mike
Feel free to respond on twitter if that is easier for you: @mikemorabito
Just better be sure…
Or you’re dead meat.
wahahaha
hai hai.
Hahaha.
You better be sure with that…
And start working on the logo of ChanLu! Grrrrrrr
ah! Yes yes, the logo… This week, promise.
Wow, it’s amazing how you can write a tutorial like this now.
It sure is easy.
Too bad about the pangram though, just don’t give up. Continue on solving the pangram problem.
If I only have free time, I’d like to toy with CURL as well…
Yea. Thanks.
I’m not giving up with pangram. Even it takes a decade to solve.