阿里云获取token(亲测可用)
·
/*
* author:kai
* $return token
* 功能:获取阿里云token
*/
public function getalyuntoken($accesskeyid,$accessKeysecret){
date_default_timezone_set("Etc/GMT");
$one = md5(time());
$parms = array(
"AccessKeyId" => $accesskeyid,
"Action" => "CreateToken",
"Version" => "2019-02-28",
"Format" => "JSON",
"RegionId" => "cn-shanghai",
"Timestamp" => date("Y-m-d\TH:i:s\Z"),
"SignatureMethod" => "HMAC-SHA1",
"SignatureVersion" => "1.0",
"SignatureNonce" => substr($one,0,8)."-".substr($one,8,4)."-".substr($one,12,4)."-".substr($one,16,4)."-".substr($one,20,4),
);
ksort($parms);
$query = http_build_query($parms);
$stringToSign = "GET&%2F&" . urlencode($query);
$signature = urlencode(base64_encode(hash_hmac("sha1",$stringToSign,$accessKeysecret."&",true)));
$queryStringWithSign = "Signature=". $signature ."&". $query;
$tokenUrl = "http://nls-meta.cn-shanghai.aliyuncs.com/?".$queryStringWithSign;
$token_string = $this->urlOpen($tokenUrl);
$token_object = json_decode($token_string,true);
$token = $token_object['Token']['Id'];
return $token;
}
/*
* author:kai
*/
public function urlOpen($url,$data = null,$header =null){
$ch =curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
//curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
//curl_setopt($ch,CURLOPT_USERAGENT,$url,$ua);
if($data){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
$content = curl_exec($ch);
$error = curl_error($ch);
if($error){
die($error);
}
curl_close($ch);
return $content;
}
更多推荐
已为社区贡献1条内容
所有评论(0)