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]['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; } /** * 获取日志 * @param string 需要的日志 * @param string 时间 * @param string 日志类型 * @param string 过滤符 * @return 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请求头 * @return array */ private function saeHeader($uri){ return array( 'Host: g.sae.sina.com.cn', 'Accept: text/plain', 'x-sae-accesskey: '.$this->accessKey, 'x-sae-timestamp: '.$this->timestamp, 'Authorization: '. $this->getAuthorization($uri) ); } /** * 获取gAuthorization */ private function getAuthorization($uri){ $header = array( 'x-sae-timestamp' => $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 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( 'service'=>'http', 'date'=>'2015-07-03', 'ident'=>'access', 'fop'=>'head/1/5' ); $arr2 = array( 'service'=>'http', 'date'=>'2015-07-03', 'ident'=>'access', 'fop'=>'head/1/5' ); $ret = $test->getLog($arr1,$arr2); var_dump($ret);
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!