SAE real-time Log Interface SDK Usage example _php tips

Source: Internet
Author: User
Tags auth php programming php regular expression

This article describes the SAE real-time Log Interface SDK usage. Share to everyone for your reference, specific as follows:

Sina SAE is the first public cloud platform developed by Sina Research and Development Center in China, and it is also more mature since 2009, opening a lot of interfaces and services for developers to use. This time in order to facilitate the developer debugging analysis, SAE new real-time log query interface. In the future, you can filter the log information through the API and download the required real-time logs. But the Sina SAE official only gives the Python implementation, here gives the PHP version of the interface to invoke the SDK

Class saeapihandler{/** * definition AccessKey/private $accessKey;
  /** * Definition Secretkey * * Private $secretKey;
  /** * Define time stamp/private $timestamp;
    /** * Constructor/Public function __construct ($key, $sec) {$this->accesskey = $key;
    $this->secretkey = $sec;
  $this->timestamp = time ();
    /** * Overloaded Get method/Public Function __call ($name, $arg) {$ret = array ();
      if (Is_array ($arg [0])) {$len = count ($arg); For ($i =0 $i < $len; $i + +) {$ret [$i] = $arg [$i] [' FOP ']? $this-> $name ($arg [$i] [' Service '], $arg [$i] [' Date ']
      , $arg [$i] [' ident '], $arg [$i] [' FOP ']): $this-> $name ($arg [$i] [' Service '], $arg [$i] [' Date '], $arg [$i] [' ident ']); }else{$ret = $arg [3]? $this-> $name ($arg [0], $arg [1], $arg [2], $arg [3]): $this->get ($arg [0], $arg [1], $arg [
    2]);
  return $ret; /** * Get log * @param string required log * @param string time * @param string log type * @param string filter * @retu
 RN Array */ Private Function GetLog ($service, $date, $ident, $fop =null) {if ($FOP) {$uri = '/log/'. $service. ' /'. $date. ' /'. $_server[' http_appversion ']. ' -'. $ident. '. Log? '.
    $FOP; }else{$uri = '/log/'. $service. ' /'. $date. ' /'. $_server[' http_appversion ']. ' -'. $ident. '.
    Log ';
    $ret = Explode (Php_eol, $this->get ($uri));
    Array_splice ($ret, 0,7);
    Array_pop ($ret);
  return $ret;
    Private function Get ($uri) {$host = ' http://g.sae.sina.com.cn '. $uri;
    $ch = Curl_init ();
    curl_setopt ($ch, Curlopt_url, $host);
    curl_setopt ($ch, Curlopt_returntransfer, 1);
    curl_setopt ($ch, Curlopt_httpheader, $this->saeheader ($uri));
    curl_setopt ($ch, Curlopt_header, 1);
    $ret = curl_exec ($ch);
    Curl_close ($ch);
  return $ret; /** * SAE Request Header * @return Array */Private Function Saeheader ($uri) {return Array (' Host:g.sae.sin A.com.cn ', ' accept:text/plain ', ' X-sae-accesskey: '. $this->accesskey, ' X-sae-timestamp: '. $this->timestamp, ' Authorization: '.
  $this->getauthorization ($uri)); /** * Get gauthorization/Private Function getauthorization ($uri) {$header = array (' X-sae-timestam
    P ' => $this->timestamp, ' X-sae-accesskey ' => strtolower ($this->accesskey));
    Ksort ($header);
    $sae _header = Array (' Get ', $uri);
    foreach ($header as $key => $value) {$sae _header[count ($sae _header)] = $key. ': $value;
    } $ret = Implode (Php_eol, $sae _header);
    $auth = ' saev1_hmac_sha256 '. Base64_encode (Hash_hmac (' sha256 ', $ret, $this->secretkey,true));
  return $auth;

 }
}

It is also very simple to instantiate the Saeapihandler class and Invoke the Getlog () method. This method can pass array parameters or strings, can be seen in the SAE document, if you need to return more than one group of logs, then pass multiple arrays.

$test = new Saeapihandler (sae_accesskey,sae_secretkey);
$arr 1 = Array (
  ' service ' => ' http ',
  ' date ' => ' 2015-07-03 ', '
  ident ' => ' access ',
  ' fop ' => ') HEAD/1/5 '
  );
$arr 2 = Array (
  ' service ' => ' http ',
  ' date ' => ' 2015-07-03 ', '
  ident ' => ' access ',
  ' FOP ' => ' HEAD/1/5 '
  );
$ret = $test->getlog ($arr 1, $arr 2);
Var_dump ($ret);

More about PHP Interested readers can view the site topics: "PHP common Database Operation skills Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator usage Summary" and "PHP string (String) Usage Summary"

I hope this article will help you with the PHP program design.

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.