以下是阿里签名机制的说明  以下是PHP具体实现方法封装
class register{
static $AccessKeyId = 'Your accessKeyId';
static $AccessKeySecret = 'Your accessKeySecret';
static $AliHost = 'http://domain.aliyuncs.com/?';
public static function search($domain){
$param = self::getCommonParams();
$param['Action'] = 'CheckDomain';
$param['DomainName'] = $domain;
$param['Signature'] = self::getSignature($param);
}
private static function getCommonParams(){
date_default_timezone_set("GMT");
$data = [
'Format' => 'JSON',
'Version' => '2016-05-11',
'AccessKeyId' => self::$AccessKeyId,
'SignatureMethod' => 'HMAC-SHA1',
'Timestamp' => date(DATE_ISO8601),
'SignatureVersion' => '1.0',
'SignatureNonce' => microtime(),
];
return $data;
}
private static function percentEncode($str)
{
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
private function getSignature($parameters)
{
ksort($parameters);
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' .self::percentEncode($key)
. '=' . self::percentEncode($value);
}
$stringToSign = 'GET&%2F&' . self::percentencode(substr($canonicalizedQueryString, 1));
$signature = base64_encode(hash_hmac('sha1', $stringToSign, self::$AccessKeySecret . '&', true));
return $signature;
}
public static function httpCurl($url, $query = [], $withHeader = false, $header = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
if ($withHeader) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if (!empty($query)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
}
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$response = curl_exec($ch);
$errorNo = curl_errno($ch);
is_resource($ch) && curl_close($ch);
if (0 !== $errorNo) {
throw new \Exception('curl error,errno:'.$errorNo);
}
return $response;
}
}
|