zoukankan      html  css  js  c++  java
  • php实现阿里云签名类

    <?php
    namespace AppLibraryAli;
    class VideoInterface{
        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-dTH:i:s'),
            );
            //判断输入的参数是否为数组
            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"=>"GetVideoPlayAuth",
        "VideoId"=>"---"
    ];
    $url = "http://vod.cn-shanghai.aliyuncs.com/?";
    $obj = new videoInterface($arr,$url);
    print_r($obj->callInterface());
    

      


    转自 https://blog.csdn.net/weixin_38422478/article/details/77750896

  • 相关阅读:
    cocos2dx3.1从零学习(二)菜单、场景切换、场景传值
    XCode5添加新建类模板(Cocos2dx Template Class for Scene or Layer)
    根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换
    如何学习 cocos2d-x ?
    Java数据类型中String、Integer、int相互间的转换
    Android各种效果集合
    重新生成IE02
    nvl与 is not null的区别等
    自定义view
    select into from 和 insert into select 的用法和区别(转)
  • 原文地址:https://www.cnblogs.com/llkbk/p/12166501.html
Copyright © 2011-2022 走看看