HTTP POST curl php时的GDMS(Grandstream)API问题

gfttwv5a  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(152)

我尝试通过curl在php中发送HTTP POST请求到新的Grandstream API,每次都得到相同的错误。我不得不说,我目前能够成功地执行GET请求,但当一个主体必须在POST中发送时,问题就开始了。

$gdms_domain = "eu.gdms.cloud";
$timestamp = round(microtime(true)*1000);

$params_data = array(
        'access_token' => '*********',
        'client_id' => '*****',
        'client_secret' => '************',
        'timestamp' => $timestamp
);
$body_data = array(
        'pageSize' => "",
        'pageNum' => "",
        'order' => "",
        'type' => "",
        'orgId' => ""
);
$params = http_build_query($params_data);
$body = json_encode($body_data);
$signature = hash("sha256","&".$params."&");

$payload_data = array(
    'access_token' => $token,
    'signature' => $signature,
    'timestamp' => $timestamp
);
$payload = http_build_query($payload_data);

$ch = curl_init();
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_URL => 'https://'.$gdms_domain.'/oapi/v1.0.0/device/list'."?".$payload,
    CURLOPT_HTTPHEADER => array(
            "Content_type: application/json"
    )
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);

if(curl_error($ch)){
    echo 'Request Error:'.curl_error($ch);
}else{
    $response = json_decode($response);
    print_r($response);
    return $response;
}
curl_close($ch);

返回的错误为:

(
[data] => 
[msg] => bad signature
[retCode] => 40003
)

API文档在这里GDMSAPI。我试着根据文档用两种可能的方式构建签名,并用不同的方式发送主体。
谢谢

i86rm4rw

i86rm4rw1#

您应该将散列正文添加到签名中。

$body_signature  = hash("sha256",$body);

则最终签名应为

$signature = hash("sha256","&".$params."&".$body_signature."&");

相关问题