/** * * 访问api * @param $path api url的path部分,例如:/user/profile(path 必须以/开头) * @param $params 参数数组 * @param $method GET/POSt * @param $macKey 下发的mac key * @return array / false */ public function callApiSelfSign($path, $params = array(), $macKey, $method = 'GET') { // 获取nonce 随机数:分钟 $nonce = XMUtil::getNonce(); $path = $path; if (!array_search('clientId', $params)) { $params['clientId'] = $this->clientId; } if (!array_search('token', $params)) { $params['token'] = $this->accessToken; } $sign = XMUtil::buildSignature($nonce, $method, $this->getApiHost(), $path, $params, $macKey); $header = XMUtil::buildMacRequestHead($this->accessToken, $nonce, $sign); return $this->callApi($path, $params, false, $header, $method); }
// mac access type // token有较长的有效期,可以存储下来,不必每次去获取token var_dump($token); // 拿到token id $tokenId = $token->getAccessTokenId(); // 创建api client $xmApiClient = new XMApiClient($clientId, $tokenId); // 获取nonce 随机数:分钟 $nonce = XMUtil::getNonce(); $path = $userProfilePath; $method = "GET"; $params = array('token' => $tokenId, "clientId" => $clientId); // 计算签名 $sign = XMUtil::buildSignature($nonce, $method, $xmApiClient->getApiHost(), $path, $params, $token->getMacKey()); // 构建header $head = XMUtil::buildMacRequestHead($tokenId, $nonce, $sign); // 访问api $result = $xmApiClient->callApi($userProfilePath, $params, false, $head); // 返回json print '<br><br>'; var_dump($result); print '<br><br>'; $result = $xmApiClient->callApiSelfSign($userProfilePath, array(), $token->getMacKey()); // 返回json var_dump($result); } } else { print "Get token Error"; } } else { print "Get code error : " . $_GET["error"] . " error description : " . $_GET["error_description"];