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

Rémi Goyard wrote a nice little 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' parameter 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 */ private $GOOGLE_READER_ACTIONS = array( "subscriptions" =>"atom/user/[USERID]/pref/com.google/subscriptions",
"starred" =>"atom/user/[USERID]/state/com.google/starred",
"shared" => "atom/user/[USERID]/state/com.google/broadcast",
"broadcast" => "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
*/
private $_defaultParams = array("n"=>20);

const SERVICE_NAME = "reader";

private $_userID = "";
private $_email = "";
private $_password = "";

/**
* @var Zend_Http_Client
*/
private $_client = null;

/**
*
* @param string $email
* @param string $password
* @param string $userID
*/
public function __construct($email, $password, $userID){
if (! ($email && $password && $userID)) {
require_once 'Zend/Exception.php';
throw new Zend_Exception(
'Please set your Google credentials before trying to ' .
'authenticate');
}
$this->_email = $email;
$this->_password = $password;
$this->_userID = $userID;
$this->_setHttpClient();

}
private function _setHttpClient()
{
if (! ($this->_email && $this->_password)) {
require_once 'Zend/Exception.php';
throw new Zend_Exception(
'Please set your Google credentials before trying to ' .
'authenticate');
}
$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(
'maxredirects' => 0,
'strictredirects' => true,
'useragent' => self::DEFAULT_SOURCE.Zend_Version::VERSION
)
);
$this->_client->setUri(self::CLIENTLOGIN_URI);
$this->_client->setMethod("POST");
$response = $this->_client->request();
if($response->getStatus() == 200){
foreach (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
*/
public function import($type, $params = array())
{
if(array_key_exists($type, $this->GOOGLE_READER_ACTIONS)){
if(! empty($params)){$this->_defaultParams = $params;}
// TODO : TAG ...
// TODO : manage 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 implemented');
}
}
}

6 Replies to “Zend Framework Class to retreive your RSS feeds used in Google Reader”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.