When starting out developing a Hi5 app, I found examples of how to make rest calls nil to none. As such, I've decided to do my civic duty and post an example of a hi5 wrapper class, and an example using it. I hope it helps people get an easier start than I had.
/** * Constructs the connection class, and obtains a valid Hi5AuthToken * * @param string $aKey The API_Key each application has when its created * @param string $aUsername Username of the developer of the app * @param string $aPassword Password of the developer of the app * */ public function __construct($aKey,$aUsername, $aPassword) { $this->sKey = $aKey; $this->aUsername = $aUsername; $this->aPassword = $aPassword;
/** * Obtains the profile of the specified user * * @param int $iUser A valid userid of the profile being requested * * @return array containing the user profile */ public function user($iUser) { $sQuery = $this->sServer.'json/profile/user/'.$iUser; return $this->do_request($sQuery, array('Hi5AuthToken'=>$this->aToken), "GET", "json"); }
/** * Obtains the friends of the specified user * * @param int $iUser UserID of the person who's friends are requested * * @return array contining the list of friends or $iUser */ public function friends($iUser) { $sQuery = $this->sServer.'json/profile/foaf/'.$iUser; return $this->do_request($sQuery, array('Hi5AuthToken'=>$this->aToken), "GET", "json"); }
/** * Sends either a "GET", or "POST" request along with the specified parameters * * @param string $sQuery UserID of the person who's friends are requested * @param array $data Array containing the parameters for the "GET" or "POST" * request where $key=>$val in the array map to ?var=val * respectively in the "GET" or "POST" request. * @param string $post "GET" or "POST * @param string $json "json" or "xml" indicating the type of response that * is returned from the request * * @return array containing the result of the query */ public function do_request($sQuery, $data, $post, $json){ $parameter_string = $this->create_parameter_string($data);
if (strtoupper($post) == "POST"){//do post $context = array("http" => array("method" => "POST", "header" => "Content-type: application/x-www-form-urlencoded\r\n". "Content-length: " . strlen($parameter_string), "content" => $parameter_string)); $contextid=stream_context_create($context); $sock=fopen($sQuery, 'r', false, $contextid); }else{//do get $sQuery .= "?$parameter_string"; $sock=fopen($sQuery, 'r', false); } if ($sock) { $result=''; while (!feof($sock)) $result.=fgets($sock, 4096);
/** * creates an http-style parameter string from the given array * * @param array $params Array containing the parameters for the "GET" or "POST" * request where $key=>$val in the array map to ?var=val * respectively in the "GET" or "POST" request. * * @return string containing the http-style parameter string */ public function create_parameter_string($params) { $post_params = array();
foreach($params as $key=>$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); } return implode('&', $post_params); }
/** * converts the given xml string to an array * * @param string $sxml XML string to be converted to an array * * @return array converted from the XML string */ public static function convert_simplexml_to_array($sxml) { $arr = array(); if ($sxml) { foreach ($sxml as $k => $v) { if ($sxml['count']) { $arr[] = self::convert_simplexml_to_array($v); } else { $arr[$k] = self::convert_simplexml_to_array($v); } } } if (sizeof($arr) > 0) { return $arr; } else { return (string)$sxml; } } } ?>
The source got all screwed up in index.php, to see it clearly, in firefox, you can high-light the post, right click, and select "View Selection Source". That will show you the formatted source.
I want to get the status and using following function in the above mentioned class
public function getStatus($iUser) { $sQuery = $this->sServer.'json/status/getStatus'; return $this->do_request($sQuery, array('Hi5AuthToken'=>$this->aToken), "GET", "json"); }
and getting error:
Warning: fopen(http://api.hi5.com/json/status/getStatus?Hi5AuthToken=SkiyD0pIsg8a5LejAQAAAA..%3AjbfjRSyiEGDcyF5cchEdjkF91tyIg4eVHNK1XAA0saECA7sclMs-_iiDEXljtwgbcFBjNikNZqQ9G5EkS4gDSRpCJBeQDk3LPCDUDclAsp4.) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in E:\wwwroot\yash\statusUpdater\hi5\index.php on line 88
I need to find out the userid when i provide username,passoword,apikey. Its obvious that user will not enter the userid of his hi5 account. We need to find out as we have username/ password