下面的代码自己写的,报错AuthFailure.SignatureFailure,The provided credentials could not be validated. Please check your signature is correct.麻烦帮看看哪里的问题,谢谢
<?php
header('Content-type:text/html; Charset=utf-8');
$SecretId = 'xxx';
$SecretKey = 'xxx';
$uriData = 'tmt.tencentcloudapi.com';
$SourceText = "hello world!";
$Source = "en";
$Target = "zh";
$region = 'ap-chengdu';
$test = new fanyi($SecretId, $SecretKey, $uriData);
$ret = $test->translate($SourceText, $Source, $Target, $region);
$ret = json_decode($ret, true);
print_r($ret);
class fanyi {
protected $SecretId;
protected $SecretKey;
public function __construct($SecretId, $SecretKey, $uriData) {
$this->SecretId = $SecretId;
$this->SecretKey = $SecretKey;
$this->uriData = $uriData;
}
public function translate($SourceText, $Source, $Target, $region) {
$arr = array(
'Action' => 'TextTranslate',
'ProjectId' => 0,
'Region' => $region,
'SecretId' => $this->SecretId,
'SourceText' => urlencode($SourceText),
'Source' => $Source,
'Target' => $Target,
'Nonce' => mt_rand(10000, 99999),
'Timestamp' => time(),
'Version' => '2018-03-21'
);
$arr['Signature'] = urlencode(self::sign($this->uriData, $arr, $this->SecretKey));
print_r($arr);
return self::curlPost("https://$this->uriData", $arr);
}
public static function sign($uriData, $params, $SecretKey){
ksort($params, SORT_STRING);
$params = http_build_query($params);
$srcStr = "$uriData/?$params";
$signStr = base64_encode(hash_hmac('sha1', $srcStr, $SecretKey, true));
return $signStr;
}
public static function curlPost($url = '', $postData = '', $options = array()) {
if (is_array($postData)) {
$postData = http_build_query($postData, true);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
相似问题