獲取阿里雲許可權類,請求阿里雲API
阿新 • • 發佈:2018-12-30
<?php
namespace Org\Util;
class Aliauth{
public $data;
public $accessKeyId = "";
public $accessKeySecret = "";
public $url;
public function __construct($actionArray,$url){
$this->url = $url;
date_default_timezone_set("GMT");
$this->data = array(
// 公共引數
'Format' => 'json',
'Version' => '2017-03-21',
'AccessKeyId' => $this->accessKeyId,
'SignatureVersion' => '1.0',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce'=> uniqid(),
'TimeStamp' => date('Y-m-d\TH:i:s\Z'),
);
//判斷輸入的引數是否為陣列
if(is_array($actionArray)){
$this->data = array_merge($this->data,$actionArray);
}
}
public function percentEncode($str){
// 使用urlencode編碼後,將"+","*","%7E"做替換即滿足ECS API規定的編碼規範
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res );
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
public function computeSignature($parameters, $accessKeySecret){
// 將引數Key按字典順序排序
ksort($parameters);
// 生成規範化請求字串
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' . $this->percentEncode($key)
. '=' . $this->percentEncode($value);
}
// 生成用於計算簽名的字串 stringToSign
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
// 計算簽名,注意accessKeySecret後面要加上字元'&'
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
return $signature;
}
public function callInterface(){
// 計算簽名並把簽名結果加入請求引數
$this->data['Signature'] = $this->computeSignature($this->data, $this->accessKeySecret);
// 傳送請求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url . http_build_query($this->data));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$res = json_decode($res);
return $res;
}
}
// $arr = [
// "Action"=>"DeleteCategory",
// "CateId"=>"857164610"
// ];
// $url = "http://vod.cn-shanghai.aliyuncs.com/?";
// $obj = new videoInterface($arr,$url);
// print_r($obj->callInterface());