Sending Authenticated Data to the Twitter API

Just Posted: Apple Likely to Give Education Rather than Resolution for iPhone 4 Antenna Issues

In my previous jQuery tutorial I discussed how you can authenticate against the Twitter API to retrieve personal data. However, what we didn’t do is show you how to POST data back to Twitter, like tweeting. It’s actually very simple, and can be done by quickly updating your cURL script in PHP.

cURL POSTFIELDS

<?php

$username = "yourusername";
$password = "yourpassword";
$twitterHost = "http://twitter.com/statuses/update.xml";
$yourStatus = "Hello - I am Tweeting from my custom script!";
$curl;

$curl = curl_init();

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=". urlencode(stripslashes(urldecode($yourStatus))));
curl_setopt($curl, CURLOPT_URL, $twitterHost);

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200) {

echo "Success!"

} else {

echo "We have failed... oh no!";

}
curl_close($curl);

?>

This script is very very similar to the last one we discussed. All we have changed is the Twitter API url, and added an additional cURL statement to add POSTFIELDS to the cURL call. Very lovely!

Conclusion

That just about wraps up my 3 part tutorial on how to work with the Twitter API with PHP and jQuery. I hope you enjoyed the set, and please feel free to add your own feedback and comments below.

Further Reading on this topic…

Developing the Twitter API

Developing the Twitter API

Practical Web2.0 with PHP

Practical Web2.0 with PHP

PHP Web Development

PHP Web Development

Trackbacks/Pingbacks

  1. [...] post by The Daily of the University of Washington and software by Elliott Back Categories: Uncategorized Posted By: Last Edit: 13 Feb 2009 [...]

  2. [...] Sending Authenticated Data to the Twitter API [...]

Leave a Reply