[foaf-dev] [Fwd: Fwd: Social Graph api client in diso repository]

Paul Scott pscott at uwc.ac.za
Mon Feb 4 04:44:24 GMT 2008


On Sat, 2008-02-02 at 23:47 -0800, Dan Brickley wrote:
> Some handy PHP...
> 

Improved to work better in PHP5, make use of built in JSON extension,
and alternative use of fopen or curl:

<?php
/**
 * Social Graph API URL
 *
 */
define ('APIURL','http://socialgraph.apis.google.com/lookup');

/**
 * Enter description here...
 *
 */
class socialgraphapi 
{
	/**
	 * edgesout - Return edges out from returned nodes.
	 *
	 * @var boolean
	 */
	public $edgesout;
	
	/**
	 * Edges in - Return edges in to returned nodes
	 *
	 * @var boolean
	 */
	public $edgesin;
	
	/**
	 * Follow me? - Follow me links, also returning reachable nodes
	 *
	 * @var boolean
	 */
	public $followme;
	
	/**
	 * sgn - Return internal representation of nodes
	 *
	 * @var boolean
	 */
	public $sgn;
	
	/**
	 * Returnable data
	 *
	 * @var object
	 */
	public $data;
	
	/**
	 * Constructor
	 *
	 * @param array $params array of parameters
	 */
	public function __construct($params)
	{
		$this->edgesout = isset($params['edgesout']) ? $params['edgesout'] :
'0';
		$this->edgesin = isset($params['edgesin']) ? $params['edgesin'] : '0';
		$this->followme = isset($params['followme']) ? $params['followme'] :
'0';
		$this->sgn = isset($params['sgn']) ? $params['sgn'] : '0';
	}
		
	/**
	 * Method to get the data back from Google
	 *
	 * @param string $uris - the uri that we are interested in
	 * @param string $method - the method to use to get the data
	 * @return stdClass Object
	 */
	function grab($uris, $method = 'curl') 
	{
		// is array? implode else pass
		if (is_array($uris)) {
			$uris = implode(',',$uris);
		}
		if (empty($uris)) {
			return null;
		}
		else {
			$qs = '';
			$qs .= 'q=' . $uris . 
			   '&edo=' . $this->edgesout .
			   '&edi=' . $this->edgesin .
			   '&fme=' . $this->followme .
			   '&sgn=' . $this->sgn;
		}
		if($method == 'curl')
		{
			$ch = curl_init(APIURL."?$qs");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$result = curl_exec($ch);
			curl_close($ch);
		
			if (empty($result)) 
			{
				return null;
			}
		
			//TODO: handle errors
			$this->data = json_decode($result);
			return $this->data;
		}
		elseif($method == 'fopen') {
			$handle = fopen(APIURL."?$qs", 'r');
			$data = fgets($handle, 100000);
			$this->data = json_decode($data);
			return $this->data;
		}
		
		
	}
}


-------------- next part --------------
All Email originating from UWC is covered by disclaimer 
http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm 


More information about the foaf-dev mailing list