我尝试写接口文档调用,但是报错
$class->get('sysinfo','Network.Info')
//报错
{"ACTION":"sysinfo","CODE":"MissingNonce","MESSAGE":"Missing nonce!"}
下面是核心代码
public function get($appName, $actonName){
$options = $this->options($appName, $actonName);
$request = $this->client->request('GET','/?api_action='.$appName,[
'timeout'=>10,
'verify'=>false,
'query'=>$options
]);
$body = false;
try{
if($request->getStatusCode() == 200){
$body = $request->getBody();
}else{
$this->error = $request->getStatusCode();
}
}catch (RequestException $exception){
$this->error = $exception->getMessage();
}
return $body;
}
private function options($appName, $actonName){
$time = time();
$options = [
'api_action'=>$actonName,//API 接口名称
'api_agent_app'=>$appName,//受控端应用代号
'api_format'=>'json',
'api_lang'=>'zh_cn',
'api_timestamp'=>$time,
'api_nonce'=>substr(md5($time),6,16),//随机字符
'api_debug'=>$this->isDubug
];
$options['api_sign'] = $this->sign($options);
}
private function sign($options)
{
$data = [];
ksort($options);
foreach ($options as $_k=>$_v){
if (is_array($_v)){
asort($_v);
}
$data[$_k] = $_v;
}
$query = http_build_query($data);
return hash_hmac('md5', $query, $this->signKey);
}