阿里云CDN API调用

官方接口文档:https://help.aliyun.com/document_detail/27149.html?spm=5176.doc27148.6.147.xqxQBK

请求参数设置:

 

function setParameter($specialParameter){
   $time = date('Y-m-d H:i:s', time()-60*60);
   $var = strtr($time, ' ', 'T');
   $Timestamp = $var . 'Z';

   $signature_nonce = '';
   for($i =0 ; $i < 14; $i++)
      $signature_nonce .= mt_rand(0,9);

   $publicParameter = array(
      'Format'         => 'JSON',
      'Version'        => '2014-11-11',
      'SignatureMethod'   => 'HMAC-SHA1',
      'TimeStamp'         => $Timestamp,
      'SignatureVersion'  => '1.0',
      'SignatureNonce'    => $signature_nonce,
   );

   $parameter = array_merge($publicParameter, $specialParameter);
   return $parameter;
}

带签名的url:

 

 

function getStringToSign($parameter,$access_key_secret){
   ksort($parameter);
   foreach($parameter as $key => $value){
      $str[] = rawurlencode($key). "=" .rawurlencode($value);
   }
   $ss = "";
   if(!empty($str)){
      for($i=0; $ifunction curl_get($url){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // https请求 不验证证书和hosts
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
   curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
   curl_setopt($ch, CURLOPT_TIMEOUT, 15);
   $output = curl_exec($ch);
   curl_close($ch);
   return $output;
}

 

以查询CDN服务状态为例:

 

function describeCdnService($access_key_id, $access_key_secret){
   if(empty($access_key_id) || empty($access_key_secret)){
      $message = array(
         'status'   => 'failed',
         'reason'   => 'Access key Id or access key secret is invalid',
      );
      return json_encode($message);
   }
   $action = 'DescribeCdnService';
   $specialParameter = array(
      'AccessKeyId'     => $access_key_id,
      'Action'         => $action,
   );

   $parameter = setParameter($specialParameter);
   $url = getStringToSign($parameter,$access_key_secret);

   $ret = curl_get($url);

   return $ret;
}

正确返回如下:

 

{

 "OpeningTime":"yyyy-mm-ddTh:i:sZ",

 "RequestId":"XXXXXXXXXXXXXXX",

 "OperationLocks":{"LockReason":[]},

 "InternetChargeType":"PayByBandwidth"

}

 

你可能感兴趣的:(PHP学习笔记)