xxx of yyy characters
Join Group

Hi5 Platform Development

Google Translation: Off

When Google Translation is on, topics and messages in this group will be machine-translated to your language by Google.

Messages


Topic: Hi5 Rest API example
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.
Dec 17, 2008
11:09 AM

Posted by Nick Thompson

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
hi5.php:
<?php
class Connection {
private $sServer = 'http://api.hi5.com/';
private $aKey;
private $aUsername;
private $aPassword;
private $aToken;

/**
* 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;

$params = array(
'api_key' => $this->sKey,
'username' => $this->aUsername,
'password' => $this->aPassword
);
$this->aToken = $this->do_request($this->sServer."rest/auth/plain",$params, "POST", "xml");
}

/**
* 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);

fclose($sock);
}

if (strtolower($json) == "json"){
$hResult = json_decode($result);
}else{
$oXML = @simplexml_load_string($result); // Warnings for "invalid" xmlns
$hResult = self::convert_simplexml_to_array($oXML);
}
return $hResult;
}

/**
* 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;
}
}
}
?>
Dec 17, 2008
11:09 AM

Posted by Nick Thompson

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
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.
Dec 17, 2008
11:19 AM

Posted by Nick Thompson

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
index.php: <?php require_once('hi5.php'); $api_key = 'XXXXXXXXXXXXXXXX'; $username = "XXXXXXXXXX@XXXXXX.com"; $password = 'XXXXX'; $userID = 'XXXXXXX'; $s = new Connection($api_key, $username, $password); $hProfile = $s->user($userID); echo '
'; 
  print_r($hProfile) ;
  echo '
'; $hProfile = $s->friends($userID); echo '
'; 
  print_r($hProfile) ;
  echo '
'; ?>
Dec 17, 2008
11:21 AM

Posted by Nick Thompson

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
I was just looking for something like this (although I would need to write in Java). This is very helpful. Thanks!

I agree, better documentations is needed from Hi5
Dec 17, 2008
11:39 AM

Posted by Alex Li

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
Thanks for the useful example! I have tried and success with Update status.

Unfortunately i cannot complete when try to publish an image. I use this api to upadte: http://api.hi5.com/rest_.feed.photos.user.%7BuserId%7D.html

I am very appreciate for any comments.

Thanks
May 7, 2009
4:30 AM

Posted by Tin Nguyen

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
Nice code, ty very much. just 1 question.
Why you change to json instead of rest for the friend list.

I need to do it with rest but i cant make it work
May 12, 2009
9:09 AM

Posted by Andres Luque

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
Hi,

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


Please let me know the solution

Thanks
Jun 29, 2009
10:22 PM


Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
Hi,

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

Can any one let me know.
thanks
Jun 30, 2009
7:12 AM


Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  
dude it works...but u need to specify the userId..!
Sep 8, 2009
6:26 AM

Posted by ashish patel

Note: To add an image or link in your message, use <img src="http://www.hi5.com/example.jpg"> or <a href="http://www.hi5.com/example.html">Text </a>

 
 

  

Title
body
 

Purchase additional coins

You need an additional: hi5 Coins hi5 Coins

Get Coins No Thanks