zoukankan      html  css  js  c++  java
  • 关于下载SAE日志签名认证的方法——PHP版

      之前需要下载SAE上的日志存入数据库,因此研究了下SAE的签名认证和日志下载。这个链接是SAE官方给出的API文档。https://www.sinacloud.com/doc/api.html#qian-ming-fang-fa,虽然讲的方法很清晰,但并不是很具体。然后Python版本的网上比比皆是,我这里不费过多笔墨,看他的官方文档也不困难。

       下面的方法是获得SAE的签名认证:

     1   public function getAuthorization($accesskey,$secretkey,$time,$uri)
     2   {
     3     $header = array(
     4                 'x-sae-timestamp' => $time,
     5                 'x-sae-accesskey' => strtolower($accesskey)
     6             );
     7         ksort($header);
     8         $sae_header = array('GET',$uri);
     9         foreach ($header as $key => $value) {
    10             $sae_header[count($sae_header)] = $key.':'.$value;
    11         }
    12         $ret = implode(PHP_EOL, $sae_header);
    13         $auth = 'SAEV1_HMAC_SHA256 '.base64_encode(hash_hmac('sha256',$ret,$secretkey,true));
    14         return $auth;
    15   }

    代码解释:传入参数L:$accesskey,$secretkey,$time,$uri将所有x-sae-开头的http header(小写)按name排序后用冒号链接name和value,放在method、uri(包含query string,不要转义)后以换行符分隔拼接为签名原文字符串,再以Secretkey为密钥用hmac sha256计算哈希值(二进制),再经base64编码,前面加上”SAEV1_HMAC_SHA256 “,得到本次请求的认证签名,即Authorization字段。

      获得签名认证之后还需要获得header,方法如下:

     1   public function getheader($accesskey,$auth,$time)
     2   {
     3     return array(
     4                 'Host: g.sae.sina.com.cn',
     5                 'Accept: text/plain',
     6                 'x-sae-accesskey: '.$accesskey,
     7                 'x-sae-timestamp: '.$time,
     8                 'Authorization: '. $auth
     9             );
    10   }

    得到header之后就非常简单了:可以直接发送请求来获取SAE的日志:
     1   public function get($uri,$header){
     2     $host = 'http://g.sae.sina.com.cn'.$uri;
     3     $ch = curl_init();
     4     curl_setopt($ch, CURLOPT_URL,$host);
     5     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     6     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     7     curl_setopt($ch, CURLOPT_HEADER, 1);
     8     $ret =  curl_exec($ch);
     9     curl_close($ch);
    10     return $ret;
    11   }

    使用上面的方法在配合具体的下载要求就可以随意下载SAE上的日志了。

    下面附上完整的代码实例,这只是按照我的要求下载的日志并且存入了数据库,仅供参考:

     1     public function log()
     2     {
     3         //$uri = '/log/http/2016-08-03:2016-08-05/'.$_SERVER['HTTP_APPVERSION'].'3-access.log';
     4         $date = date('Y-m-d',strtotime('-1 day'));
     5         $time = time();
     6         $acc = 'lwjljzn2wz';
     7         $sec = '1hkzhw2y51wl2w1i21hl4miyj2yj44l5x1404l00';
     8         $uri = '/log/http/'.$date.'/3-access.log?grep/3.dxever.applinzi.com.*POST|fields/ /1/2/5';
     9         $uri_1 = '/log/http/'.$date.'/3-access.log?grep/3.dxever.applinzi.com.*POST|fields/"/2';
    10         //$uri = '/log/http/2016-08-09/3-access.log?fields/ /1/2/3';
    11         $g = D('godview');
    12         $auth = $g->getAuthorization($acc,$sec,$time,$uri);
    13         $header = $g->getheader($acc,$auth,$time);
    14         $ret = $g->get($uri,$header);
    15         $time_1 = time();
    16         $auth_1 = $g->getAuthorization($acc,$sec,$time_1,$uri_1);
    17         $header_1 = $g->getheader($acc,$auth_1,$time_1);
    18         $ret_1 = $g->get($uri_1,$header_1);
    19         $time_mom = date('Ymd',strtotime('-1 day'));
    20         $data = explode("3.dxever.applinzi.com",$ret);
    21         $data_1 = explode("POST ",$ret_1);
    22 
    23         unset($data_1[0]);
    24         unset($data[0]);
    25         foreach ($data as $k => $v)
    26         {
    27 
    28             preg_match_all('//index.php/Area/School/GetSchoolComment/token(.*) HTTP/1.0/',$data_1["$k"],$token);
    29             if ($token[1][0] != NULL) {
    30                 $log['user_id'] = $g->get_id($token[1][0]);
    31             }else {
    32                 $log['user_id'] = NULL;
    33             }
    34             preg_match_all('//(.*) HTTP/1.0/',$data_1["$k"],$url);
    35 
    36             preg_match_all('/((([1-9]?|1d)d|2([0-4]d|5[0-5])).){3}(([1-9]?|1d)d|2([0-4]d|5[0-5]))/',$v,$ip);
    37             preg_match_all('//2016:(.*)/',$v,$ti);
    38             // if ($url[1][0]==NULL) {
    39             //     // $url = 'a';
    40             //     preg_match_all('/GET (.*?) HTTP/',$v,$url);
    41             // }
    42             // preg_match_all('/Mozilla(.*)/',$v,$head);
    43             // $area = explode(" ",$time);
    44             $log['ip'] = $ip[0][0];
    45             $log['url'] = $url[1][0];
    46             $log['head'] = $head[0][0];
    47             $log['area'] = $area[1];
    48             // $time = $area[0];
    49             // $t = explode(":",$time,2);
    50             $log['time1'] = $time_mom;
    51             $log['time2'] = str_replace(':','',$ti[1][0]);
    52             // dump($log);
    53             $sta = $g->save_log($log);
    54         }
    55         if($sta){
    56             $this->response(returnSuccess(), 'json');
    57         }else {
    58             $this->response(returnWrong(), 'json');
    59         }
    60 
    61     }
    下面为用到的方法的全部整合:
     1  public function getAuthorization($accesskey,$secretkey,$time,$uri)
     2   {
     3     $header = array(
     4                 'x-sae-timestamp' => $time,
     5                 'x-sae-accesskey' => strtolower($accesskey)
     6             );
     7         ksort($header);
     8         $sae_header = array('GET',$uri);
     9         foreach ($header as $key => $value) {
    10             $sae_header[count($sae_header)] = $key.':'.$value;
    11         }
    12         $ret = implode(PHP_EOL, $sae_header);
    13         $auth = 'SAEV1_HMAC_SHA256 '.base64_encode(hash_hmac('sha256',$ret,$secretkey,true));
    14         return $auth;
    15   }
    16 
    17   public function getheader($accesskey,$auth,$time)
    18   {
    19     return array(
    20                 'Host: g.sae.sina.com.cn',
    21                 'Accept: text/plain',
    22                 'x-sae-accesskey: '.$accesskey,
    23                 'x-sae-timestamp: '.$time,
    24                 'Authorization: '. $auth
    25             );
    26   }
    27 
    28   public function get($uri,$header){
    29     $host = 'http://g.sae.sina.com.cn'.$uri;
    30     $ch = curl_init();
    31     curl_setopt($ch, CURLOPT_URL,$host);
    32     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    33     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    34     curl_setopt($ch, CURLOPT_HEADER, 1);
    35     $ret =  curl_exec($ch);
    36     curl_close($ch);
    37     return $ret;
    38   }
    39 
    40   public function save_log($log)
    41   {
    42     $a = M('log')->add($log);
    43     return $a;
    44   }
     
  • 相关阅读:
    安装k8s和NVIDIA环境
    docker使用阿里云加速器
    Ubuntu16安装NVIDIA驱动后重复登录 简单粗暴
    Ubuntu TTY 字体大小 目录颜色 中文乱码 设置
    vim使用记录
    k8s内运行ubuntu容器
    IRelationalOperator接口
    IArea接口(计算多边形面积)
    ITopologicalOperator接口
    通过ArcEngine接口建立凸包(一)
  • 原文地址:https://www.cnblogs.com/linuxzxy/p/6406497.html
Copyright © 2011-2022 走看看