本文实例讲述了sae实时日志接口sdk用法。分享给大家供大家参考,具体如下:
新浪sae是新浪研发中心开发的国内首个公有云平台,从2009年开始到现在也是也来越成熟,开放了很多接口以及服务供开发者使用。这次为了方便开发者调试分析,sae新增实时日志查询接口。今后您可以通过api对日志信息进行筛选,并下载所需的实时日志。但是新浪sae官方只给出的python的实现,这里给出php版本的接口调用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;
}
}
使用也很简单,实例化saeapihandler类,调用getlog()方法即可。该方法可以传递数组参数或者字符串,具体可以到sae文档看,如果需要返回多组日志,则传递多个数组即可。
$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);
希望本文所述对大家php程序设计有所帮助。
更多sae实时日志接口sdk用法示例。