Location: PHPKode > scripts > Gravatar RPC > gravatar-rpc/GravatarRPC.class.php
<?php
/**
 * Wrapper class for the Gravatar XMLRPC api
 * Encapulates all methods from the api
 * @author Wouter van Vliet / Interpotential
 * @email hide@address.com
 * @url http://www.interpotential.com
 * @version 1.0
 * @date Sep 24 2009
 * @see http://en.gravatar.com/site/implement/xmlrpc for more information
 *      about the API
 */
class GravatarRPC {

	protected $client;
	protected $email;
	protected $apikey;

	/**
	 * Constructor
	 * @param $apikey string The apikey belonging to the account we're 
	 *  	  working with. You can get your apikey on the edit profile page
	 *		  on wordpress.com
	 * @param $email string The email address the apikey matches with
	 */
	public function __construct($apikey, $email) {
		require_once 'XML/RPC.php';

		$this->apikey = $apikey;
		$this->email = strtolower(trim($email));
	}

	/**
	 * Construct a client object
	 */
	protected function client() {

		if ($this->client === null) {
			$this->client = new XML_RPC_Client('/xmlrpc?user='.md5($this->email), 'secure.gravatar.com', 443);
		}

		return $this->client;
	}
	/**
	 * Call a method
	 * @param $method Method name, automatically gets "grav." prepended to it
	 * @param $params Array Array of named arguments
	 */
	protected function call($method, $params = Array()) {

		#$params = array_map('XML_RPC_encode', $params);
		$params = array_merge($params, Array(
			'apikey'	=> $this->apikey,
		));
		$msg = new XML_RPC_Message("grav.$method", Array(XML_RPC_encode($params)));
		$response = $this->client()->send($msg);
		if (!$response->faultCode()) {
			$value = $response->value();
			$value = XML_RPC_decode($value);
			return $value;
		} else {
			throw new GravatarException($response->faultString(), $response->faultCode());
		}
	}

	/**
	 * Check whether a hash has a gravatar
	 * @param $emails Array list of email addresses to check
	 */
	public function exists($emails = Array()) {

		if (!is_array($emails)) {
			$emails = Array($emails);
		}

		$emails = array_map(function($mail) {
			return strtolower(md5(strtolower(trim($mail))));
		}, $emails);

		$params = Array($emails);

		return $this->call('exists', Array('hashes' => $emails));
	}

	/**
	 * Return an array of userimages for this account 
	 */
	public function userimages() {
		$resp = $this->call('userimages');
		foreach($resp as $hash => $info) {
			$resp[$hash] = (object)Array(
				'rating'	=> $info[0],
				'url'		=> $info[1],
			);
		}
		return $resp;
	}
	/**
	 * Save binary image data as a userimage for this account
	 * @param $data blog A data blog to save as image
	 * @param $rating int Rating of this image, 0 = g, 1 = pg, 3 = x
	 * @param $slurp boolean Treat $data as string path to filename, slurp this
	 *   			 and send it to Gravatar
	 * @returns mixed boolean(false) on failure, image string on success
	 */
	public function saveData($data, $rating, $slurp = false) {

		$rating = (int)$rating;
		if ($rating < 0 || $rating > 3) throw new InvalidArgumentException('Rating should be a number from 0 to 3');

		if ($slurp == true) {

			if (!is_file($data) || !is_readable($data)) {
				throw new InvalidArgumentException('Path provided is not a readable file.');
			} 
			$data = file_get_contents($data);
		}

		return $this->call('saveData', Array(
			'rating' => $rating,
			'data'	=> base64_encode($data),
		));
	}
	/**
	 * Read an image via its URL and save that as a userimage for this account 
	 * @param $url string A full url to an image on the internet
	 * @param $rating int Rating of this image, 0 = g, 1 = pg, 2 = r, 3 = x
	 * @returns mixed boolean(false) on failure, image string on success
	 */
	public function saveUrl($url, $rating) {

		$rating = (int)$rating;
		$url = (string)$url;
		if ($rating < 0 || $rating > 3) throw new InvalidArgumentException('Rating should be a number from 0 to 3');

		return $this->call('saveUrl', Array(
			'rating' => $rating,
			'url'	=> $url,
		));
	}
	/**
	 * Use a userimage as a gravatar for one of more addresses on this account
	 * @param $image string The userimage you wish to use
	 * @param $email string|array Single email address as string, or list of
	 * 		  email addresses to use this image for
	 * @returns Array List of statusses per email address or single boolean if
	 *  		email was supplied as single string
	 */
	public function useUserimage($image, $email) {

		$returnSingle = false;
		$image = (string)$image;
		if (is_string($email)) {
			$returnSingle = strtolower(trim($email));
			$email = Array($email);	
		}
		if (!is_array($email)) {
			throw new InvalidArgumentException('Email address is not a string or array of email addresses');	
		}

		// give all email addresses in lowercase
		$email = array_map(function($e) {
			return strtolower(trim($e));
		}, $email);

		$value = $this->call('useUserimage', Array(
			'userimage'	=> $image,
			'addresses'	=> $email,
		));

		// if we came with a string, we only want one return value
		if ($returnSingle && isset($value[$returnSingle])) {
			return $value[$returnSingle];
		}

		return $value;

	}
	/**
	 * Get a list of addresses for this account
	 */
	public function addresses() {
		$resp = array_map(function($info) {
			return (object)$info;
		}, $this->call('addresses'));

		return $resp;
	}
	/**
	 * Test the api
	 */
	public function test() {
		return $this->call('test');
	}
}
class GravatarException extends Exception { }
Return current item: Gravatar RPC