Using the Twitter API

Here's a quick HowTo on using the Twitter API. If you aren't familiar with Twitter, here's a brief description taken directly from their homepage:

Twitter is a service for friends, family, and co–workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question: What are you doing?

You might want to register in their service first since
Many Twitter API methods require authentication.

If you have cURL installed, you can already play with the Twitter API. Examples:
curl http://twitter.com/statuses/public_timeline.xml

or
curl -u username:password http://twitter.com/statuses/friends_timeline.xml

And since PHP is the only scripting language that I'm familiar and at the same time comfortable with, here's a simple snippet that does the second method mentioned above:

<?php
$user = $_GET['user'];
$password = $_GET['password'];
$url = "http://twitter.com/statuses/friends_timeline.xml";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, false );
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "count=20");
$xml = curl_exec($ch);
curl_close($ch);

header('content-type: text/xml');

// print out xml
echo $xml;
?>

Usage:
http://somedomain/path/to/file.php?user=username&password=password

References:

0 comments: