首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CDN API 刷新url接口 为什么 一直返回“身份认证失败,用户身份验证失败”?

CDN API 刷新url接口 为什么 一直返回“身份认证失败,用户身份验证失败”?

提问于 2019-05-31 17:12:37
回答 0关注 1查看 293

已经按照调用实例给的写了...


public function sendSX($url){
    $timestamp  = time();
    $host_real_name = Db::name('config')->where(['name'=>'host_real_name'])->value('value');
    $secretId  = Db::name('config')->where(['name'=>'cdn_id'])->value('value');
    $secretKey = Db::name('config')->where(['name'=>'cdn_secret'])->value('value');
    $action='RefreshCdnUrl';
    $HttpUrl="cdn.api.qcloud.com";
    /*除非有特殊说明,如MultipartUploadVodFile,其它接口都支持GET及POST*/
    $HttpMethod="POST";
    /*是否https协议,大部分接口都必须为https,只有少部分接口除外(如MultipartUploadVodFile)*/
    $isHttps =true;
    $COMMON_PARAMS = array(
        'Nonce'             => rand(10000,99999),
        'Timestamp'         => $timestamp,
        'Action'            => $action,
        'SecretId'          => $secretId,
        'SignatureMethod'   => 'HmacSHA256',
        'urls.0'            => $url
    );
    $PRIVATE_PARAMS = array();
    $res = $this->CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps);
    $jsonRes = json_decode($res,true);
    return $jsonRes;
}

function CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps)
{
    $FullHttpUrl = $HttpUrl."/v2/index.php";

    /***************对请求参数 按参数名 做字典序升序排列,注意此排序区分大小写*************/
    $ReqParaArray = array_merge($COMMON_PARAMS, $PRIVATE_PARAMS);
    ksort($ReqParaArray);
    /**********************************生成签名原文**********************************
     * 将 请求方法, URI地址,及排序好的请求参数  按照下面格式  拼接在一起, 生成签名原文,此请求中的原文为
     * GETcvm.api.qcloud.com/v2/index.php?Action=DescribeInstances&Nonce=345122&Region=gz
     * &SecretId=AKIDz8krbsJ5yKBZQ    ·1pn74WFkmLPx3gnPhESA&Timestamp=1408704141
     * &instanceIds.0=qcvm12345&instanceIds.1=qcvm56789
     * ****************************************************************************/
    $SigTxt = $HttpMethod.$FullHttpUrl."index.php?";

    $isFirst = true;
    foreach ($ReqParaArray as $key => $value)
    {
        if (!$isFirst)
        {
            $SigTxt = $SigTxt."&";
        }
        $isFirst= false;

        /*拼接签名原文时,如果参数名称中携带_,需要替换成.*/
        if(strpos($key, '_'))
        {
            $key = str_replace('_', '.', $key);
        }

        $SigTxt=$SigTxt.$key."=".$value;
    }
    /*********************根据签名原文字符串 $SigTxt,生成签名 Signature******************/
    $Signature = base64_encode(hash_hmac('sha256', $SigTxt, $secretKey, true));


    /***************拼接请求串,对于请求参数及签名,需要进行urlencode编码********************/
    $Req = "Signature=".urlencode($Signature);
    foreach ($ReqParaArray as $key => $value)
    {
        $Req=$Req."&".$key."=".urlencode($value);
    }

    /*********************************发送请求********************************/
    if($HttpMethod === 'GET')
    {
        if($isHttps === true)
        {
            $Req="https://".$FullHttpUrl."?".$Req;
        }
        else
        {
            $Req="http://".$FullHttpUrl."?".$Req;
        }

        $Rsp = file_get_contents($Req);

    }
    else
    {

        if($isHttps === true)
        {

            $Rsp= $this->SendPost("https://".$FullHttpUrl,$Req,$isHttps);
        }
        else
        {
            $Rsp= $this->SendPost("http://".$FullHttpUrl,$Req,$isHttps);
        }
    }

    return $Rsp;
}

function SendPost($FullHttpUrl, $Req, $isHttps)
{

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $Req);

    curl_setopt($ch, CURLOPT_URL, $FullHttpUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($isHttps === true) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false);
    }

    $result = curl_exec($ch);

    return $result;
}

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档