TwitterQuery: My Lightweight Twitter API Library For PHP
In my free time for the past couple of weeks I have been doing a lot of messing around with Twitter. I've been writing some neat little apps that I think will make my Twitter experience more enjoyable. I'm not sure what makes Twitter so addictive to people, but for me it is definitely the simplicity and power of the Twitter API. I am sure it is the main reason I use Twitter as much as I do.
As you may know, my first taste of the Twitter API was when I wrote a script to display your Twitter status updates on your website. Since then I journeyed deeper into the API, experienced more of the features and developed somewhat of a feel for using the API to create apps. One thing I noticed in the beginning when trying to decide if I should use a Twitter library was that most I came across were needlessly abstracted. They often contained one method for each API call which in turn passes the url of that call to a query method that actually talks to the Twitter website. I think the way the Twitter API was implemented makes it easy enough to simply call that query method and provide the url ourselves. This is exactly what I propose in (drum roll please) TwitterQuery, my lightweight version of a Twitter API library:
<?php
//
// TwitterQuery.class.php
//
class TwitterQuery
{
private
$auth = null,
$username = null,
$password = null;
public function setBasicAuth ($username, $password)
{
$this->auth = 1;
$this->username = $username;
$this->password = $password;
}
public function query ($url, $options = array(), $method = "get")
{
$url = sprintf('http://twitter.com/%s.json', $url);
if ($method == "get" && count($options) > 0)
{
$url .= "?".http_build_query($options);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ($method == "post")
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($options));
}
if ($this->auth)
{
curl_setopt($curl, CURLOPT_USERPWD, "$this->username:$this->password");
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$twitter_result = curl_exec($curl);
curl_close($curl);
return json_decode($twitter_result, true);
}
}
?>
Download Source: TwitterQuery.class.php
Pretty simple eh?. It is what I will be using in the next few articles I write detailing some of the neat apps I have developed for Twitter. I also plan on figuring out how Twitter's new OAuth capabilities work and integrating that into an updated version of TwitterQuery.
But I am betting you want to take this new library for a test drive. Well returning to the article I wrote on displaying Twitter status updates on your website, using this new "library", we can rewrite the code as follows:
<?php
function twitter_updates ($username, $password)
{
$twitterQuery = new TwitterQuery();
$twitterQuery->setBasicAuth($username, $password);
$results = $twitterQuery->query("statuses/user_timeline");
$status = array();
foreach ($results as $result)
{
$status[] = array("screen_name" => $result["user"]["screen_name"], "created_at" => strtotime($result["created_at"]), "text" => $result["text"]);
}
return $status;
}
?>
Download Source: twitter_updates.php



