<?php 
  

	function getAccessToken($api_key,$api_secret){
		
		$result = array();
		
		$requestParam = array("api_key"=>$api_key,"api_secret"=>$api_secret);
		$service_url = API_ENDPOINT_GETACCESSTOKEN;
		 
		$data = processRequest($service_url,$requestParam);
		$result[API_STATUS_STR] = $data[API_STATUS_STR];
		
		 
	
		if($data[API_STATUS_STR] == API_SUCCESS_CODE){
			$result[API_DATA_STR] = $data[API_DATA_STR]["api_token"];
		} else {
			$result[API_DATA_STR] = $data[API_DATA_STR];
		}
	
		return $result;
	}

	function getProducts($api_key,$api_token){
		$requestParam = array("api_key"=>$api_key,"api_token"=>$api_token);
		$service_url = API_ENDPOINT_GETPRODUCTS;
	
		$data = processRequest($service_url,$requestParam);
		
		 
		
		$result[API_STATUS_STR] = $data[API_STATUS_STR];
		$result[API_DATA_STR] = $data[API_DATA_STR];
		 
	
	 
	
		return $result;
	}


	function expireToken($api_key,$api_token){
		
		$result = array();
		
		$requestParam = array("api_key"=>$api_key,"api_token"=>$api_token);
		$service_url = API_ENDPOINT_EXPIRETOKEN;
	
		$data = processRequest($service_url,$requestParam);
		$result[API_STATUS_STR] = $data[API_STATUS_STR];
		
		
	
	
		if($data[API_STATUS_STR] == API_SUCCESS_CODE){
			$result[API_DATA_STR] = $data[API_DATA_STR]["api_token"];
		} else {
			$result[API_DATA_STR] = $data[API_DATA_STR];
		}
	
		return $result;
	 
	}

	function processRequest($service_url,$requestParam){
		
		$curl = curl_init($service_url);
		
		
		$curl_post_data =  json_encode($requestParam);
		
		
		
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
		$curl_response = curl_exec($curl);
		if ($curl_response === false) {
			$info = curl_getinfo($curl);
			curl_close($curl);
			die('error occured during curl exec. Additioanl info: ' . var_export($info));
		}
		curl_close($curl);
		
		$object = json_decode($curl_response);
		
		if(!empty($object)){
			$responseData = objectToArray($object);
		}
		 
		
		$status  = $responseData[API_RESPONSE_STR][API_STATUS_STR];
		$data[API_STATUS_STR] = $status;
		if ($status == API_SUCCESS_CODE) {			
			$data[API_DATA_STR] = $responseData[API_RESPONSE_STR][API_DATA_STR];
		} else {
			$data[API_DATA_STR] = $responseData[API_RESPONSE_STR][API_ERROR_DESC_STR];
		}
		
		return $data;
	}
	
	function objectToArray($d) {
		if (is_object($d))
			$d = get_object_vars($d);
		return is_array($d) ? array_map(__METHOD__, $d) : $d;
	}

?>