You can now follow Alex’s Cocktail Making on Twitter @cocktaildaily so I’d love it if you followed me.
I have heard a lot about the twitter API lately being really really easy to use and so having hacked things together with the delicious, flickr, eBay, typepad and facebook apis in the past I thought it was time to give twitter a go. The whole development process start to finish took me 25minutes (started at 18:11 and finished at 18:36) from creating a new twitter account to having a daily updating script which pushed a random cocktail with over a certain score and a reasonable likelihood of not being rude to twitter. It took me 12minutes to write this blog post! Below are the resources that should make this easy for you to do and at the very bottom is the code I used (without my username and password)!
http://apiwiki.twitter.com/Things-Every-Developer-Should-Know (the twitter documentation, curl from command line at the bottom – it’s cool you can update twitter from command line)
http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/ (this page showed me which curl-setopt I needed to send the message (it’s a post) and is now very slightly out of date but still should get you over the hump)
http://us2.php.net/manual/en/function.curl-setopt.php (the curl documentation)
For completeness here’s the code I used… you can easily modify it to do your own:
// Set username and password
$username = 'ENTERYOURUSERNAME';
$password = 'ENTERYOURPASSWORD';// the message you want to send (I had a bunch of code here)
$message = “enter your message here”;
// The twitter API address
$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); // POST not GET
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 'there was a problem with the twitter updater';} else {};

Comments