这里有新鲜出炉的精品教程,程序狗速度看过来!
这篇文章主要介绍了 SAE 实时日志接口 SDK 用法, 结合实例形式分析了 PHP 版本的接口调用 SDK 及相应的使用方法, 需要的朋友可以参考下
本文实例讲述了 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 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0822/342173.html