本文執行個體講述了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相關內容感興趣的讀者可查看本站專題:《php常見資料庫操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《php排序演算法總結》、《PHP常用遍曆演算法與技巧總結》、《PHP資料結構與演算法教程》、《php程式設計演算法總結》、《PHP數學運算技巧總結》、《phpRegex用法總結》、《PHP運算與運算子用法總結》及《php字串(string)用法總結》
希望本文所述對大家PHP程式設計有所協助。