apiKey.$this->secretKey.time().$this->userName; // get the length $length = strlen($unencryptedSignature); //append the length to the signature $unencryptedSignature = $unencryptedSignature.$length; //MD5 on signature print("Creating a signature... \n"); $signature = md5($unencryptedSignature); return $signature; } /** * Method for parsing Nitro XML response as array of attributes and values */ public function my_xml2array($__url) { $xml_values = array(); $contents = file_get_contents($__url); $parser = xml_parser_create(''); if(!$parser) return false; xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, trim($contents), $xml_values); xml_parser_free($parser); if (!$xml_values) return array(); $xml_array = array(); $last_tag_ar =& $xml_array; $parents = array(); $last_counter_in_tag = array(1=>0); foreach ($xml_values as $data) { switch($data['type']) { case 'open': $last_counter_in_tag[$data['level']+1] = 0; $new_tag = array('name' => $data['tag']); if(isset($data['attributes'])) $new_tag['attributes'] = $data['attributes']; if(isset($data['value']) && trim($data['value'])) $new_tag['value'] = trim($data['value']); $last_tag_ar[$last_counter_in_tag[$data['level']]] = $new_tag; $parents[$data['level']] =& $last_tag_ar; $last_tag_ar =& $last_tag_ar[$last_counter_in_tag[$data['level']]++]; break; case 'complete': $new_tag = array('name' => $data['tag']); if(isset($data['attributes'])) $new_tag['attributes'] = $data['attributes']; if(isset($data['value']) && trim($data['value'])) $new_tag['value'] = trim($data['value']); $last_count = count($last_tag_ar)-1; $last_tag_ar[$last_counter_in_tag[$data['level']]++] = $new_tag; break; case 'close': $last_tag_ar =& $parents[$data['level']]; break; default: break; }; } return $xml_array; } /** * Method for accessing the attribute values like XPATH */ public function get_value_by_path($__xml_tree, $__tag_path) { $tmp_arr =& $__xml_tree; $tag_path = explode('/', $__tag_path); foreach($tag_path as $tag_name) { $res = false; foreach($tmp_arr as $key => $node) { if(is_int($key) && $node['name'] == $tag_name) { $tmp_arr = $node; $res = true; break; } } if(!$res) return false; } return $tmp_arr; } public function login() { // constructing a signature $signature = $this->getSignature(); // Construct a URL for REST API call user_login to extract Session Key $request = $this->baseURL. "method=user.login". "&apiKey=".$this->apiKey. "&userId=".$this->userName. "&ts=".time(). "&sig=".$signature; // Converting XML response attribute and values to array attributes and values $arr = $this->my_xml2array($request); // Accessing the sessionKey through XPATH $sessionKeyArray = $this->get_value_by_path($arr, 'Nitro/Login/sessionKey'); $sessionKey = $sessionKeyArray['value']; return $sessionKey; } public function logAction($sessionKey) { // Construct a URL for user logAction $request = $this->baseURL."method=user.logAction". "&sessionKey=".$sessionKey. "&userId=".$this->userName. "&tags=".$this->actionTag. "&value=".$this->value; //Converting XML response attribute and values to array attributes and values $arr = $this->my_xml2array($request); print("Logging an action... \n"); $responseArray = $this->get_value_by_path($arr, 'Nitro'); if(strcmp($responseArray['attributes']['res'],"ok")==0) { print("logAction is successful... \n"); } } public function getUserPointsBalance($sessionKey) { // Construct a URL to get point balance from user $request = $this->baseURL. "method=user.getPointsBalance". "&sessionKey=".$sessionKey. "&start=0"."&pointCategory=". $this->POINT_CATEGORY_ALL."&criteria=". $this->CRITERIA_CREDITS."&userId=Suraj"; print("Getting user point balance...\n"); //Converting XML response attribute and values to array attributes and values $arr = $this->my_xml2array($request); //Accessing the Balance attributes through XPATH and extracting points information $balanceArray = $this->get_value_by_path($arr, 'Nitro/Balance'); print($this->userName." -\t". "lifetimeBalance ".$balanceArray['attributes']['lifetimeBalance']. "\t"."points ".$balanceArray['attributes']['points']."\n"); } public function getSiteActionLeaders($sessionKey) { // Construct a URL to get action leaders $request = $this->baseURL."method=site.getActionLeaders"."&sessionKey=".$sessionKey."&tags=".$this->actionTag."&tagsOperator=".$this->TAGS_OPERATOR_OR."&criteria=".$this->CRITERIA_MAX."&returnCount=".$this->value; print("Getting Action Leaders...\n"); //Converting XML response attribute and values to array attributes and values $arr = $this->my_xml2array($request); //Accessing the Actions attributes through XPATH and extracting action leaders information $actionsArray = $this->get_value_by_path($arr, 'Nitro/actions/Action'); $actionAttributes = $actionsArray['attributes']; print("The Action Leaders are - \n"); print_r($actionAttributes['userId']."\t".$actionAttributes['value']); } }; $nitroAPISample = new NitroAPISample(); print("Calling user login... \n"); $sessionKey = $nitroAPISample->login(); //user login $nitroAPISample->logAction($sessionKey); //logAction for an user $nitroAPISample->getUserPointsBalance($sessionKey); //get point balance for an user $nitroAPISample->getSiteActionLeaders($sessionKey); //get site action leaders ?>