1. 程式人生 > >Guzzle(第三方類庫)

Guzzle(第三方類庫)

Guzzle是一個PHP的HTTP客戶端,用來輕而易舉地傳送請求,並整合到我們的WEB服務上。

詳情去看facepay 專案

 class Base
 {
     //protected $baseURL='http://150.1.1.75:8090/';
     protected $baseURL='http://150.138.1.33:8050/';
     protected $key='YG';
     protected $secret='123456';
     protected $signSecret = 'lbyg_domall';
     protected  function getJSON($url,$param){
         try{
             $param['ObKey'] = $this->key;
             $param['ObSecret'] = $this->secret;
             $sign=$this->createSign($param);

             $param['Sign']=$sign;
             $httpClient = new Client(['base_uri'=>$this->baseURL]);
             $result=$httpClient->request('GET',$url,[
                 'query' => $param
             ])->getBody()->getContents();
             $result=json_decode($result,true);
             if(empty($result)||empty($result['State'])){
                 Log::error("資料中心介面{$url}呼叫未返回資訊");
             }
             if($result['State']==-1){
                 Log::error("資料中心介面{$url}呼叫失敗:".$result['Message'].',呼叫資料:'.\GuzzleHttp\json_encode($param));
             }
             return $result;
         }catch (\Exception $e){
             Log::error("資料中心介面{$url}呼叫異常:".$e->getMessage());
             return null;
         }
         return null;
     }
     protected  function postJSON($url='',$param=[]){
         try{
             $query['ObKey'] = $this->key;
             $query['ObSecret'] = $this->secret;
             if($param){
                 $param = json_encode($param);
                 $param = base64_encode($param);
             }
             $query['Sign']=$this->createSign($param);
             $httpClient = new Client(['base_uri'=>$this->baseURL]);
             $start=microtime(true);
             $result=$httpClient->request('POST',$url,[
                 'query' => $query,
                 'body' => $param
             ])->getBody()->getContents();
             $end = microtime(true);
             $exeTime= round($end-$start,3);
             Log::error("介面:$url,執行時間:$exeTime");
             $result=json_decode($result,true);
             if(empty($result)||empty($result['State'])){
                 Log::error("資料中心介面{$url}呼叫未返回資訊");
             }
             if($result['State']==-1){
                 Log::error("資料中心介面{$url}呼叫失敗:".$result['Message'].',呼叫資料:'.\GuzzleHttp\json_encode($param));
             }
             return $result;
         }catch (\Exception $e){
             Log::error('資料中心介面呼叫異常:'.$e->getMessage());
             return null;
         }
         return null;

     }

   

 }