【PHP】腾讯cos生成对象访问url

<?php
class geturl
{
    private $_region = 'ap-xxx';
    private $_bucket = 'xxx-xxx';
    private $_secret_id = "xxx"; //"云 API 密钥 SecretId";
    private $_secret_key = "xxx"; //"云 API 密钥 SecretKey";

    //获取下载链接
    public function get_down_load_url($url)
    {
        $qSignStart = time();

        $qSignEnd = bcadd($qSignStart, 3600, 0);

        $fileUri = parse_url($url);

        $ext_arr = explode('/', $url);

        $ext = $ext_arr[count($ext_arr) - 1];

        $authorization = $this->get_authorization_down_url($this->_secret_id, $this->_secret_key, $qSignStart, $qSignEnd, $fileUri['path'], '');

        // 要下载的话就添加这个
        $down_load = '';
        // $down_load = '&response-content-type=application%2Foctet-stream&response-cache-control=max-age%3D86400&response-content-disposition=attachment%3B%20filename%3D'.$ext;

        return "https://{$this->_bucket}.cos.{$this->_region}.myqcloud.com{$fileUri['path']}?" . $authorization . "" . $down_load;
    }
    /**

     * 按COS要求从数组中获取 Signature 中 [HttpString] 内容

     * 标准格式 key=value&key=value&...

     * 数组元素按键字典排序 *

     * key转换为小写

     * value进行UrlEncode转换

     * 转换为key=value格式

     * 多对key=value之间用连接符连接

     *

     */

    public function get_http_header_string($headers)
    {

        if (!is_array($headers)) {

            return false;
        }

        try {

            $tmpArray = array();

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

                $tmpKey = strtolower($key);

                $tmpArray[$tmpKey] = urlencode($value);
            }

            ksort($tmpArray);

            $headerArray = array();

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

                array_push($headerArray, "$key=$value");
            }

            return implode('&', $headerArray);
        } catch (Exception $error) {

            return false;
        }
    }

    /**

     * 按COS要求对header_list内容进行转换

     * 提取所有key

     * 字典排序

     * key转换为小写

     * 多对key=value之间用连接符连接

     *

     */

    public function get_q_header_list($headers)
    {

        if (!is_array($headers)) {

            return false;
        }

        try {

            $tmpArray = array();

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

                array_push($tmpArray, strtolower($key));
            }

            sort($tmpArray);

            return implode(';', $tmpArray);
        } catch (Exception $error) {

            return false;
        }
    }

    /**

     * 计算签名

     * secretId、secretKey 为必需参数,qSignStart、qSignEnd为调试需要,测试通过后应取消,改为方法内自动创建

     */
    public function get_authorization_down_url($secretId, $secretKey, $qSignStart, $qSignEnd, $fileUri, $headers)
    {

        $qSignTime = "$qSignStart;$qSignEnd"; //unix_timestamp;unix_timestamp

        $qKeyTime = $qSignTime;

        $header_list = $this->get_q_header_list($headers);

        //如果 Uri 中带有 ?的请求参数,该处应为数组排序后的字符串组合

        $url_param_list = '';

        //compute signature

        $httpMethod = 'get';

        $httpUri = $fileUri;

        //与 q-url-param-list 相同

        $httpParameters = $url_param_list;

        //将自定义请求头分解为 & 连接的字符串

        $headerString = $this->get_http_header_string($headers);

        // 计算签名中的 signature 部分

        $signTime = $qSignTime;

        $signKey = hash_hmac('sha1', $signTime, $secretKey);

        $httpString = "$httpMethod\n$httpUri\n$httpParameters\n$headerString\n";

        $sha1edHttpString = sha1($httpString);

        $stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";

        $signature = hash_hmac('sha1', $stringToSign, $signKey);

        //组合结果

        $authorization = "q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$qSignTime&q-key-time=$qKeyTime&q-header-list=$header_list&q-url-param-list=$url_param_list&q-signature=$signature";

        return $authorization;
    }
}
$url = $_GET['url'];
$qcloud_class = new geturl();
$get_down_load_url = $qcloud_class->get_down_load_url($url);
echo $get_down_load_url;
die;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值