twi^&@#t!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.

Download || View Demo