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;

     }

   

 }

  

posted @ 2018-11-27 14:08  王玉岩  阅读(632)  评论(0编辑  收藏  举报