Home > php教程 > PHP开发 > SAE real-time log interface SDK usage example

SAE real-time log interface SDK usage example

高洛峰
Release: 2016-12-20 16:08:51
Original
1120 people have browsed it

The example in this article describes the usage of SAE real-time log interface SDK. Share it with everyone for your reference, the details are as follows:

Sina SAE is the first domestic public cloud platform developed by Sina R&D Center. It has become more and more mature since 2009, and has opened many interfaces and services for developers to use. This time, in order to facilitate developers’ debugging and analysis, SAE has added a new real-time log query interface. In the future, you can filter log information through the API and download the required real-time logs. However, Sina SAE officially only provides the Python implementation. Here is the PHP version of the interface calling SDK

class SaeApiHandler{
  /**
  *  定义accessKey
  */
  private $accessKey;
  /**
  *  定义secretKey
  */
  private $secretKey;
  /**
  *  定义时间戳
  */
  private $timestamp;
  /**
  *  构造函数
  */
  public function __construct($key,$sec){
    $this->accessKey = $key;
    $this->secretKey = $sec;
    $this->timestamp = time();
  }
  /**
  *  重载get方法
  */
  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][&#39;fop&#39;] ? $this->$name($arg[$i][&#39;service&#39;],$arg[$i][&#39;date&#39;],$arg[$i][&#39;ident&#39;],$arg[$i][&#39;fop&#39;]):$this->$name($arg[$i][&#39;service&#39;],$arg[$i][&#39;date&#39;],$arg[$i][&#39;ident&#39;]);
      }
    }else{
      $ret = $arg[3] ? $this->$name($arg[0],$arg[1],$arg[2],$arg[3]) : $this->get($arg[0],$arg[1],$arg[2]);
    }
    return $ret;
  }
  /**
  *  获取日志
  *  @param string 需要的日志
  *  @param string 时间
  *  @param string 日志类型
  *  @param string 过滤符
  *  @return array
  */
  private function getLog($service,$date,$ident,$fop=null){
    if ($fop) {
      $uri = &#39;/log/&#39;.$service.&#39;/&#39;.$date.&#39;/&#39;.$_SERVER[&#39;HTTP_APPVERSION&#39;].&#39;-&#39;.$ident.&#39;.log?&#39;.$fop;
    }else{
      $uri = &#39;/log/&#39;.$service.&#39;/&#39;.$date.&#39;/&#39;.$_SERVER[&#39;HTTP_APPVERSION&#39;].&#39;-&#39;.$ident.&#39;.log&#39;;
    }
    $ret = explode(PHP_EOL,$this->get($uri));
    array_splice($ret,0,7);
    array_pop($ret);
    return $ret;
  }
  private function get($uri){
    $host = &#39;http://g.sae.sina.com.cn&#39;.$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请求头
  *  @return array
  */
  private function saeHeader($uri){
    return array(
        &#39;Host: g.sae.sina.com.cn&#39;,
        &#39;Accept: text/plain&#39;,
        &#39;x-sae-accesskey: &#39;.$this->accessKey,
        &#39;x-sae-timestamp: &#39;.$this->timestamp,
        &#39;Authorization: &#39;. $this->getAuthorization($uri)
      );
  }
  /**
  *  获取gAuthorization
  */
  private function getAuthorization($uri){
    $header = array(
        &#39;x-sae-timestamp&#39; => $this->timestamp,
        &#39;x-sae-accesskey&#39; => strtolower($this->accessKey)
      );
    ksort($header);
    $sae_header = array(&#39;GET&#39;,$uri);
    foreach ($header as $key => $value) {
      $sae_header[count($sae_header)] = $key.&#39;:&#39;.$value;
    }
    $ret = implode(PHP_EOL, $sae_header);
    $auth = &#39;SAEV1_HMAC_SHA256 &#39;.base64_encode(hash_hmac(&#39;sha256&#39;,$ret,$this->secretKey,true));
    return $auth;
  }
}
Copy after login

It is also very simple to use. Just instantiate the SaeApiHandler class and call the getLog() method. This method can pass array parameters or strings. You can see the SAE documentation for details. If you need to return multiple sets of logs, just pass multiple arrays.

$test = new SaeApiHandler(SAE_ACCESSKEY,SAE_SECRETKEY);
$arr1 = array(
  &#39;service&#39;=>&#39;http&#39;,
  &#39;date&#39;=>&#39;2015-07-03&#39;,
  &#39;ident&#39;=>&#39;access&#39;,
  &#39;fop&#39;=>&#39;head/1/5&#39;
  );
$arr2 = array(
  &#39;service&#39;=>&#39;http&#39;,
  &#39;date&#39;=>&#39;2015-07-03&#39;,
  &#39;ident&#39;=>&#39;access&#39;,
  &#39;fop&#39;=>&#39;head/1/5&#39;
  );
$ret = $test->getLog($arr1,$arr2);
var_dump($ret);
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more SAE real-time log interface SDK usage examples related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template