Zend Developer Zone
Where the ElePHPants come to learn

Zend Developer Zone

NP-Gravatar

Incredibly Old Content Warning!

The content you are viewing is more than three years old. It is here for archival purposes. Do not use any advice in this article.

::Jedi Hand Wave:: This is not the information you are looking for. Move along.


Gravatar is a famous web-service for providing globally-unique avatars. Gravatar, like many other web services, has its own APIs, which are based on simple HTTP GET requests. You can find out more about those on this link.

By studying and analyzing those APIs, I come up with an idea to create a Zend Framework implementation of all those Gravatar services. That resulted in NP-Gravatar package, which will be presented in this post.

NP-Gravatar package provides classes for using and implementing Gravatar's services and features. As I did for my previous project, I hosted NP-Gravatar on Google Code and you can check it out here. Also, here is a direct link for downloading this package.

You'll find three separate components in this package, and each of them represents implementation of some Gravatar's API. Those are:

NP_Service_Gravatar_Profiles

This component is a client for performing Gravatar profile requests and retrieving profile data of some Gravatar user, based on his/her primary email address. Profile data can be returned in various formats supported by Gravatar Profiles API (JSON, XML, PHP, VCF/vCard, QR Code), and some of those formats are, by default, converted to NP_Gravatar_Profile object, which represents profile data of some user in object-oriented manner.

Here are some usage examples:

//Creating instance:
$gravatarService = new NP_Service_Gravatar_Profiles();
//Changing response format to XML:
$gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_Xml());

//Getting profile data.
$profile = $gravatarService->getProfileInfo('foo@bar.com');
//$profile is instance of NP_Gravatar_Profile so we can access some of its properties.
echo 'ID: ' . $profile->id . '<br />';
echo 'Username: ' . $profile->getPreferredUsername() . '<br /><br />';

echo 'Photos: <br />';
foreach($profile->getPhotos() as $photo) {
   echo '<img src="' . $photo->value . '" /> <br />';
}

//Changing response format to JSON:
$gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_Json());
//Getting profile data but forcing raw Zend_Http_Response object to be returned, 
//by passing boolean true for the second argument of the getProfileInfo() method:
$response = $gravatarService->getProfileInfo('foo@bar.com', true);
if ($response instanceof Zend_Http_Response) { //true!
   //do something
}

//Changing response format to QR Code:
$gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_QRCode());
//QR Code response can not be exported NP_Gravatar_Profile object, as that 
//response format type does not implement 
//NP_Service_Gravatar_Profiles_ResponseFormat_ParserInterface interface, 
//so raw Zend_Http_Response object will allways be returned when using 
//that response format:
$response = $gravatarService->getProfileInfo('foo@bar.com');
echo $response->getHeader('Content-type'); //Prints "image/png".

NP_Service_Gravatar_XmlRpc

This one is a client for the Gravatar XML-RPC API, which maps all methods provided by that API. Enough talk, usage examples will explain everything:

//Gravatar XML-RPC implementation requires API key for the 
//authentication proccess. It can be retrieved on the page 
//for editing profile, on wordpress.com.
$apiKey = 'someAPIKey'; 
$email = 'foo.bar@foobar.com'; //Email address associated with the $apiKey.
//Creating instance:
$gravatarXmlRpc = new NP_Service_Gravatar_XmlRpc($apiKey, $email);

//Checking whether there's a gravatar account registered with supplied email addresses.
$result = $gravatarXmlRpc->exists(array(
   'posa.nikola@gmail.com', //That's me. πŸ˜€
   'foo@example.com'
));
$values = array_values($result);
echo (bool)$values[0]; //Prints "true", as I do have Gravatar account. :)
echo (bool)$values[1]; //Prints "false", as that second email address probably doesn't exist.

//Getting user images on the current account:
$images = $gravatarXmlRpc->userImages();
//$image is instance of NP_Service_Gravatar_XmlRpc_UserImage, 
//as we didn't pass $raw parameter as "true" when executing 
//userImages() method.
$image = $images[0];
$imageUrl = $image->getUrl(); //Instance of Zend_Uri_Http.
echo $image->getRating(); //Prints some rating (G, PG, R or X).

//Saves some image to be a user image for the current account.
$this->_gravatarXmlRpc->saveData('path/to/someImage.jpg', NP_Service_Gravatar_XmlRpc::PG_RATED);

NP_View_Helper_Gravatar

This is a simple view helper for rendering Gravatar image URLs, which follows Gravatar Image Requests API. Again, usage examples are the best way for presenting features of this component:

//In some view script:

//Generating Gravatar URL.
echo '<img src="' . $this->gravatar('foo@bar.com') . '" />;

//Generating Gravatar URL and specifying size and rating options.
echo '<img src="' . $this->gravatar('foo@bar.com', array('s'=>200, 'r'=>'pg')) . '" />;
//Full parameter names are supported, too.
echo '<img src="' . $this->gravatar('foo@bar.com', array('size'=>100, 'default'=>'identicon')) . '" />;

//Generating Gravatar URL and specifying file-type extension.
echo '<img src="' . $this->gravatar('foo@bar.com', array('s'=>200), 'jpg') . '" />;
//Above view helper call will produce this URL:
//http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8.jpg?s=200