SAE實(shí)時(shí)日志接口SDK用法示例
本文實(shí)例講述了SAE實(shí)時(shí)日志接口SDK用法。分享給大家供大家參考,具體如下:
新浪SAE是新浪研發(fā)中心開發(fā)的國內(nèi)首個(gè)公有云平臺(tái),從2009年開始到現(xiàn)在也是也來越成熟,開放了很多接口以及服務(wù)供開發(fā)者使用。這次為了方便開發(fā)者調(diào)試分析,SAE新增實(shí)時(shí)日志查詢接口。今后您可以通過API對(duì)日志信息進(jìn)行篩選,并下載所需的實(shí)時(shí)日志。但是新浪SAE官方只給出的Python的實(shí)現(xiàn),這里給出PHP版本的接口調(diào)用SDK
class SaeApiHandler{ /** * 定義accessKey */ private $accessKey; /** * 定義secretKey */ private $secretKey; /** * 定義時(shí)間戳 */ private $timestamp; /** * 構(gòu)造函數(shù) */ 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 時(shí)間 * @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請(qǐng)求頭 * @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; } }
使用也很簡單,實(shí)例化SaeApiHandler類,調(diào)用getLog()方法即可。該方法可以傳遞數(shù)組參數(shù)或者字符串,具體可以到SAE文檔看,如果需要返回多組日志,則傳遞多個(gè)數(shù)組即可。
$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);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常見數(shù)據(jù)庫操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》及《php字符串(string)用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Laravel 5.1 on SAE環(huán)境開發(fā)教程【附項(xiàng)目demo源碼】
- ThinkPHP的SAE開發(fā)相關(guān)注意事項(xiàng)詳解
- PHP Wrapper在SAE上的應(yīng)用方法
- php實(shí)現(xiàn)SAE上使用storage上傳與下載文件的方法
- 新浪SAE搭建PHP項(xiàng)目教程
- 在SAE上搭建最新wordpress的方法
- ThinkPHP在新浪SAE平臺(tái)的部署實(shí)例
- 新浪SAE云平臺(tái)下使用codeigniter的數(shù)據(jù)庫配置
- 微信公眾平臺(tái)開發(fā)入門教程(SAE方倍工作室)
- sae使用smarty模板的方法
相關(guān)文章
PHP排序算法之快速排序(Quick Sort)及其優(yōu)化算法詳解
這篇文章主要介紹了PHP排序算法之快速排序(Quick Sort)及其優(yōu)化算法,結(jié)合實(shí)例形式分析了php快速排序的原理、實(shí)現(xiàn)方法,并分析了各種優(yōu)化技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-04-04PHP使用curl_multi_select解決curl_multi網(wǎng)頁假死問題的方法
這篇文章主要介紹了PHP使用curl_multi_select解決curl_multi網(wǎng)頁假死問題的方法,結(jié)合實(shí)例形式分析了使用curl_multi的過程中并發(fā)處理事務(wù)導(dǎo)致cpu占用率過高時(shí)的解決方法,需要的朋友可以參考下2018-08-08PHP實(shí)現(xiàn)負(fù)載均衡session共享redis緩存操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)負(fù)載均衡session共享redis緩存操作,涉及php用戶登陸、session存儲(chǔ)、判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08PHP中如何調(diào)用webservice的實(shí)例參考
本篇文章介紹了,PHP中如何調(diào)用webservice的實(shí)例參考。需要的朋友參考下2013-04-04