Simple Status Update in Twitter Using PHP

Posted in Did you know? on April 28th, 2008

Justin RainsWelcome! Thank you for visiting!
Follow me at Twitter
Subscribe to my full feed.

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.



< ?

$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.

Please share my posts that is why I write: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Facebook
  • Technorati
  • StumbleUpon



If you found this page useful, consider linking to it.
Simply copy and paste the code below into your web site (Ctrl+C to copy)
It will look like this: Simple Status Update in Twitter Using PHP

No Comments »

No comments yet.


RSS feed for comments on this post. TrackBack URI



Leave a comment




  • I Share

      Share Feed: Subscribe
  • Recent Visitors

  • Blogroll

  • Cool Stuff