Simple Status Update in Twitter Using PHP

by Justin on April 28, 2008

Here is a sample PHP snippet that will let a twitter user update their status using PHP code. I have it simply saying hello today is and the date. I’m thinking about running it from a cron script in an automated fashion in conjunction with another site that I run. To see the code I used click the more button unless you’re luckily enough to be sent to this post directly.


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
< ?

$today = date("l F j, Y");
// Set username and password
$username = 'username';
$password = 'password';

// The message you want to send
$message = 'Hello today is '.$today;

// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';
// Set up and execute the curl process

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
}
?>

What are some cool ideas for this? I’m thinking maybe it could tweet today’s weather forecast or something. I would like to get more in-depth and create an interactive bot just for fun.

Leave a Comment

Previous post:

Next post: