A few days ago, I asked a question on Stack Overflow regarding the Google Reader API.  What I wanted to do was build a WordPress plug-in that would import my feeds from Google Reader into my dashboard.  I've already pulled in my site statistics and Facebook, Google Reader is the last holdout keeping me from managing all of my online stuff in one spot.

Unfortunately, the best tutorial I could find online (and the one recommended by Stack Overflow users) was written for C# ... WordPress runs in PHP.  The two languages might look the same, but it would be like trying to put a chunk of Shakespearean writing in the middle of a modern book - out of context things would just break down.  So I set out to build my own version of a Google Reader reader in PHP.  Here's how to do it:

Step 1: Get an SID and Token

Google Reader authenticates users based on their username (Gmail address) and password.  After that, the system gives you an SID, which you can in turn use to get a token.  All of these elements work together to make sure you are really you and have permission to read, modify, and delete items from your Google Reader feed.  Here's the code you use to get an SID and token:

class JDMReader {
private $_username;
private $_password;
private $_sid;

private $_token;
private $_cookie;

public function __construct($username, $password) {
$this->_username = $username;
$this->_password = $password;

$this->_connect();
}

private function _connect() {
$this->_getToken();
return $this->_token != null;
}

private function _getToken() {
$this->_getSID();
$this->_cookie = "SID=" . $this->_sid . "; domain=.google.com; path=/";

$url = "http://www.google.com/reader/api/0/token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
curl_setopt($ch, CURLOPT_URL, $url);

ob_start();

curl_exec($ch);
curl_close($ch);

$this->_token = ob_get_contents();
ob_end_clean();
}

private function _getSID() {
$requestUrl = "https://www.google.com/accounts/ClientLogin?service=reader&Email=" . urlencode($this->_username) . '&Passwd=' . urlencode($this->_password);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

ob_start();

curl_exec($ch);
curl_close($ch);
$data = ob_get_contents();
ob_end_clean();

$sidIndex = strpos($data, "SID=")+4;
$lsidIndex = strpos($data, "LSID=")-5;

$this->_sid = substr($data, $sidIndex, $lsidIndex);
}
}

This set of functions forms the core of a new PHP class I've built for my Google Reader plug-in.  When you create a new instance of the class, you pass in your Google username and password.  From that, the class will get an SID, get a token, and create a cookie string that it will pass to the server every time you make a subsequent request.  It's a fairly simple system, and translates Martin Doms' C# approach into PHP as closely as possible.

I've used this already to get my Google Reader feed.  It works quickly and effectively.  Now I just need to wrap it all in a nice widget and share it with the rest of the world.  All of the power of Google Reader, right in your WordPress dashboard!

Step 2: Try it Out!

The majority of API calls you can make involve a GET request, so I built a helper function into my class to handle those calls:


private function _httpGet($requestUrl, $getArgs) {
$url = sprintf('%1$s?%2$s', $requestUrl, $getArgs);
$https = strpos($requestUrl, "https://");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($https === true) curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);

ob_start();

try {
curl_exec($ch);
curl_close($ch);
$data = ob_get_contents();
ob_end_clean();
} catch(Exception $err) {
$data = null;
}
return $data;
}

This function works well enough, but unless you have something to get with your GET, it's pretty useless. So I added a public method to my class that returns a list of all my subscriptions in a JSON array:


public function listAll() {
$gUrl = "http://www.google.com/reader/api/0/stream/contents/user/-/state/com.google/reading-list";
$args = sprintf('ck=%1$s', time());

return $this->_httpGet($gUrl, $args);
}

Now you're off and running. Two lines of code will print out your reading list:


$reader = new Reader('myname@gmail.com', 'mypassword');
echo $reader->listAll();

Easy, isn't it? Now it's your turn to try things out ...