ali 媒体转码签名生成-php
更新:HHH   时间:2023-1-7


1.封装函数

function composeUrl($AccessKeyId,$Format,$SignatureMethod,$SignatureVersion,$TimeFormat,$ActionName,$Version,$InputJson,$AccessSecret){

        $apiParams = array();

        $apiParams["AccessKeyId"] = $AccessKeyId;

        $apiParams["Format"] = $Format;

        $apiParams["SignatureMethod"] = $SignatureMethod;

        $apiParams["SignatureVersion"] = $SignatureVersion;

        $apiParams["SignatureNonce"] = uniqid();

        date_default_timezone_set("GMT");

        $apiParams["Timestamp"] = date($TimeFormat);

        $apiParams["Action"] = $ActionName;

        $apiParams["Version"] = $Version;

        $apiParams["Input"]= $InputJson;

        $apiParams["Signature"] = util::computeSignature($apiParams, $AccessSecret, $apiParams);


        $requestUrl = "http://mts.cn-shanghai.aliyuncs.com/?";


        foreach ($apiParams as $apiParamKey => $apiParamValue){

            $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";

        }

        return substr($requestUrl, 0, -1);

}



function computeSignature($parameters, $accessKeySecret, $iSigner){

        ksort($parameters);

        $canonicalizedQueryString = '';

        foreach($parameters as $key => $value){

            $canonicalizedQueryString .= '&' . util::percentEncode($key). '=' . util::percentEncode($value);

        }

        $stringToSign = 'GET&%2F&' . util::percentencode(substr($canonicalizedQueryString, 1));

        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret."&", true));


        return $signature;

}



function percentEncode($str){

        $res = urlencode($str);

        $res = preg_replace('/\+/', '%20', $res);

        $res = preg_replace('/\*/', '%2A', $res);

        $res = preg_replace('/%7E/', '~', $res);

        return $res;

}



2.调用函数并请求

$TimeFormat = 'Y-m-d\TH:i:s\Z';

$Format= 'JSON';

$Version = '2014-06-18';

$AccessKeyId = 'xxxxxxxxxxxxxxxx';//替换为你们在ali申请的Access Key ID

$AccessSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//替换为你们在ali申请的Access Key Secret

$SignatureMethod = 'Hmac-SHA1';

$SignatureVersion = '1.0';

$ActionName = 'SubmitMediaInfoJob';

$InputJson = json_encode(array("Bucket"=>"test-2-20161024","Location"=>"oss-cn-shanghai","Object"=>"user-dir/avi.avi"));


$SubmitMediaInfoJobRequest = util::composeUrl($AccessKeyId,$Format,$SignatureMethod,$SignatureVersion,$TimeFormat,$ActionName,$Version,$InputJson,$AccessSecret);


print_r($SubmitMediaInfoJobRequest);


3.根据第2步生成的链接,直接复制到地址中进行请求即可看到结果.



4.注意:

   a.在步骤1的composeUrl函数中的requestUrl这个变量,需要与步骤2的转递参数Location中的所在区域操持一致,比如:$requestUrl = ""oss-cn-shanghai".这里面都有shanghai这个区域.">http://mts.cn-shanghai.aliyuncs.com/?";那么"Location"=>"oss-cn-shanghai".这里面都有shanghai这个区域.

返回web开发教程...