阿里云openapi签名实现代码(基于PHP)

strToSign的构造过程以如下流程简要说明:
阿里云openapi签名实现代码(基于PHP)

<?php
$accessKeyId="yourAccesskeyID";
$accessKeySecret="yourAccessKeySecret";
$h_8_time=time()-8*60*60;
$Action='GetVideoPlayAuth';
$VideoId='xxxx';

//公共请求参数+Action和Action参数
$get_key = array('Format','Version','AccessKeyId','SignatureMethod','Timestamp','SignatureNonce','SignatureVersion','Action','VideoId');
//添加上述公共参数以及Action及其参数的值
$get_key_value = array('Format'=>'JSON','Version'=>'2017-03-21','SignatureMethod'=>'HMAC-SHA1','SignatureNonce'=>time()+rand(50000,100000),'SignatureVersion'=>'1.0','AccessKeyId'=>$accessKeyId,'Timestamp'=>date('Y-m-d',$h_8_time).'T'.date('H:i:s',$h_8_time).'Z','Action'=>$Action,'VideoId'=>$VideoId);
//字典序升序排序
sort($get_key);

$StringToSign='GET&'.urlencode('/').'&';
$q_str='';
//对每一个参数值进行urlencode
foreach($get_key as $kval){
    $q_str .= urlencode($kval).'='.urlencode($get_key_value[$kval]).'&';
}

$q_str = substr($q_str,0,-1);
$StringToSign .= urlencode($q_str);
echo($StringToSign."\n");
$ok_url='http://vod.cn-shanghai.aliyuncs.com?'.$q_str.'&Signature='.urlencode(base64_encode(hash_hmac("sha1",$StringToSign,$accessKeySecret.'&',true)));
echo($ok_url."\n");
?>
上一篇:图像基本处理算法的简单实现(三)


下一篇:hdu-4638-Group(树状数组)