'g', 's' => '80', 'd' => 'http://www.gravatar.com/avatar/', ); /** * Construct a new Gravatar Web Service Client * * @param string $email * @param array $params */ public function __construct($email = '', $params = null) { $this->_email = $email; if (null !== $params) { $this->setParams($params); } Zend_Service_Gravatar::getHttpClient()->setConfig(array('maxredirects' => 0)); } /** * Set parameters * * @param array $params */ public function setParams($params) { $this->_params = array_merge($this->_params, $params); return $this; } /** * Get params * * @return array Array of parameters */ public function getParams() { return $this->_params; } /** * Set an e-mail address * * @param string $email */ public function setEmail($email) { $this->_email = $email; return $this; } /** * Get an e-mail adress * * @return string E-mail address */ public function getEmail() { return $this->_email; } /** * Check if an avatar for e-mail adress is valid * * Returns TRUE when avatar exists (succesful request without redirect) * Returns FALSE when avatar doesn't exist (succesful request with redirect) * Throws exception when something goes wrong, e.g. unsuccesful request. * * @return boolean * @throws Zend_Service_Exception when response is other than successful or redirect */ public function isValid() { $client = self::getHttpClient(); $client->setUri($this->_generateUri()); $client->setMethod(Zend_Http_Client::GET); $response = $client->request(); if ($response->isSuccessful()) { return true; } if ($response->isRedirect()) { return false; } throw new Zend_Service_Exception('HTTP ' . $response->getStatus()); } /** * Get escaped URI of gravatar image. * * @return string escaped URI of gravatar image */ public function getUri() { return $this->_generateUri('&'); } /** * Get generated Gravatar ID * * @return string Gravatar ID generated using e-mail adress */ public function getGravatarId() { return md5($this->_email); } /** * Generates URI of gravatar image. * * @return string URI of gravatar image */ protected function _generateUri($separator = '&') { $uri = self::API_URI . self::PATH_AVATAR . $this->getGravatarId() . '.jpg'; $params = $this->getParams(); if (count($params)) { $uri .= '?' . http_build_query( $params, null, $separator ); } return $uri; } }