Methods for downloading SAE log signature authentication-PHP version, sae log

Source: Internet
Author: User

Methods for downloading SAE log signature authentication-PHP version, sae log

We need to download logs from SAE to the database. Therefore, we have studied signature authentication and log download for SAE. This link is an API document officially provided by SAE. Bytes. Then the Python version is everywhere on the Internet. I don't have to pay too much attention here, and it's not difficult to read his official documents.

The following method is used to obtain the signature authentication of 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   }

Code Description: input parameter L: $ accesskey, $ secretkey, $ time, $ uri, first fill all http headers starting with x-sae-(lower case) sort by name and link the name and value with a colon. Place the strings in method and uri (including query string, do not escape) and splice them with line breaks to form the original signature string, use Secretkey as the key and use hmac sha256 to calculate the hash value (Binary). Then, use base64 encoding and add "SAEV1_HMAC_SHA256" to obtain the authentication signature of this request, that is, the Authorization field.

After obtaining signature authentication, you also need to obtain the header as follows:

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}

After obtaining the header, It is very simple: you can directly send a request to obtain the log of the 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   }

You can use the above method to download logs on the SAE at will in combination with the specific download requirements.

The complete code example is attached below. This is only the log downloaded according to my requirements and saved to the database. It is for reference only:

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 ('ymmd', strtotime ('-1 Day'); 20 $ data = explod E ("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]? | 1 \ d) \ d | 2 ([0-4] \ d | 5 [0-5]) \.) {3} ([1-9]? | 1 \ d) \ 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 ['region'] = $ 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}
The following shows the full integration of the methods used:
 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: '. $auth25             );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   }

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.