Get Google Reader feeds with PHP
Here is a PHP script for pulling in unread Google Reader feeds. Reposted from Dave Shea’s blog.
// ----------------------------------------
// Google Reader Authentication in PHP
// a basic script to get you in the door of
// Google's unofficial Reader API
// by Dave Shea, mezzoblue.com
// ----------------------------------------
// cobbled together from notes on:
// http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
// these are the urls we'll need to access various services
$urlAuth = "https://www.google.com/accounts/ClientLogin";
$urlAtom = "http://www.google.com/reader/atom";
// our array of login data
$login = array(
"service" => "reader",
"continue" => "http://www.google.com/",
// Google id-only of the account holder
// ie. for example@gmail.com, just use example
"Email" => "google-id",
// the account's password in plaintext
"Passwd" => "password",
// an identifying name for your script, can be anything
"source" => "my reader script",
);
// first step is to authenticae
// let's build a POST request using the login data array
$postRequest = "";
foreach($login as $field => $value) {
$postRequest .= $field . "=" . $value . "&";
}
// start buffering what we get back
ob_start();
$ch = curl_init($urlAuth);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec ($ch);
curl_close ($ch);
// throw the buffer into a variable
$loginResult = ob_get_contents();
ob_end_clean();
// we just received three lines of ugliness to contend with.
// each line is a huge string preceded with an ID
// the IDs are: SID, LSID, and Auth; we only want SID
// let's use some string parsing to weed it out
if ($i = strstr($loginResult, "LSID")) {
$SID = substr($loginResult, 0,
(strlen($loginResult) - strlen($i)));
$SID = rtrim(substr($SID, 4, (strlen($SID) - 4)));
}
// so we've found the SID
// now we can build the cookie that gets us in the door
$cookie = "SID=" . $SID .
"; domain=.google.com; path=/; expires=1600000000";
// this builds the action we'd like the API to perform
// in this case, it's getting our list of unread items
$action = $urlAtom .
"/user/-/state/com.google/reading-list";
// note that the hyphen above is a shortcut
// for "the currently logged-in user"
// start buffering what we get back
ob_start();
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $action);
curl_setopt ($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
curl_exec ($ch);
curl_close ($ch);
// throw the buffer into a variable
$xml = ob_get_contents();
ob_end_clean();
// and finally, let's take a look.
echo $xml;
Comments
No one's commented yet. Add yours!
Add Comment