Zend Framework Class to retreive your RSS feeds used in Google Reader

Rémi Goyard wrote a nice lit­tle class to access your Google Reader Feeds take a peek below, he says;

I finally wrote my own class to retreive data from my Google reader Account (stared items, shared items, …)
The class needs some more work.
Regards
Rémi

< ?php
/**
* Class to retreive your rss feeds used in Google Reader
* thanks to :
* @see http://www.niallkennedy.com/blog/2005/12/google-reader-api.html
* @see http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
*
* @author Rémi Goyard
* @category Core
* @package Core_Gdata
* @subpackage Gdata
* @copyright Rémi Goyard
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version 0.1
*/
/**
* Zend_Gdata_HttpClient
*/
require_once 'Zend/Http/Client.php';

/**
* Zend_Version
*/
require_once ‘Zend/Version.php’;

class Core_Gdata_Reader{

/**
* The Google client login URI
*/
const CLIENTLOGIN_URI = ‘https://www.google.com/accounts/ClientLogin’;

/**
* The default ‘source’ para­me­ter to send to Google
*/
const DEFAULT_SOURCE = ‘Zend-ZendFramework-Google-Reader 1.0/Zend’;

/**
* The Google reader Base URI
*/
const GOOGLE_READER_BASE_URI = “http://www.google.com/reader/”;

/**
* Default Google Reader Actions
*/
pri­vate $GOOGLE_READER_ACTIONS = array(
“sub­scrip­tions” =>“atom/user/[USERID]/pref/com.google/subscriptions”,
“starred” =>“atom/user/[USERID]/state/com.google/starred”,
“shared” => “atom/user/[USERID]/state/com.google/broadcast”,
“broad­cast” => “atom/user/[USERID]/state/com.google/broadcast”,
“read” => “atom/user/[USERID]/state/com.google/read”,
“reading-list” => “atom/user/[USERID]/state/com.google/reading-list”,
“tag” => “atom/user/[USERID]/label/[TAG]“
);

/**
* Default Params
*/
pri­vate $_defaultParams = array(“n”=>20);

const SERVICE_NAME = “reader”;

pri­vate $_userID = “”;
pri­vate $_email = “”;
pri­vate $_password = “”;

/**
* @var Zend_Http_Client
*/
pri­vate $_client = null;

/**
*
* @param string $email
* @param string $pass­word
* @param string $userID
*/
pub­lic func­tion __construct($email, $pass­word, $userID){
if (! ($email && $pass­word && $userID)) {
require_once ‘Zend/Exception.php’;
throw new Zend_Exception(
‘Please set your Google cre­den­tials before try­ing to ’ .
‘authen­ti­cate’);
}
$this->_email = $email;
$this->_password = $pass­word;
$this->_userID = $userID;
$this->_setHttpClient();

}
pri­vate func­tion _setHttpClient()
{
if (! ($this->_email && $this->_password)) {
require_once ‘Zend/Exception.php’;
throw new Zend_Exception(
‘Please set your Google cre­den­tials before try­ing to ’ .
‘authen­ti­cate’);
}
$this->_client = new Zend_Http_Client();
$this->_client->setParameterPost(“Email”, $this->_email);
$this->_client->setParameterPost(“Passwd”, $this->_password);
$this->_client->setParameterPost(“Service”, self::SERVICE_NAME);
$this->_client->setConfig(array(
‘maxredi­rects’ => 0,
‘stric­tredi­rects’ => true,
‘user­a­gent’ => self::DEFAULT_SOURCE.Zend_Version::VERSION
)
);
$this->_client->setUri(self::CLIENTLOGIN_URI);
$this->_client->setMethod(“POST”);
$response = $this->_client->request();
if($response->getStatus() == 200){
fore­ach (explode(“\n”, $response->getBody()) as $l) {
$l = chop($l);
if ($l) {
list($key, $val) = explode(‘=’, chop($l), 2);
$this->_client->setCookie($key,$val );
}
}
return true;
}else{
$this->_client = null;
require_once ‘Zend/Exception.php’;
throw new Zend_Exception(‘Authentication Error got Response Code : ‘.$response->getStatus());
}
}

/**
* Import Atom Feed
*
* @param String $type request type
* @param Array $params request params
* @return Zend_Feed_Abstract
*/
pub­lic func­tion import($type, $params = array())
{
if(array_key_exists($type, $this->GOOGLE_READER_ACTIONS)){
if(! empty($params)){$this->_defaultParams = $params;}
// TODO : TAG
// TODO : man­age POST actions
$urlId = str_replace(“[USERID]”, $this->_userID, $this->GOOGLE_READER_ACTIONS[$type] );
$url = self::GOOGLE_READER_BASE_URI.$urlId;
$this->_client->resetParameters();
$this->_client->setMethod(“GET”);
require_once ‘Zend/Feed.php’;
Zend_Feed::setHttpClient($this->_client);
return Zend_Feed::import($url);
}else{
require_once ‘Zend/Exception.php’;
throw new Zend_Exception(‘The action ‘.$type.’ does not exists or is not yet imple­mented’);
}
}
}

Tags: ,

6 Responses to "Zend Framework Class to retreive your RSS feeds used in Google Reader"

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes