PHP獲取第三方接口,GET/POST提交方式

背景:PHP利用GET/POST提交方式向第三方傳遞數據並獲取第三方接口返回值 

  1. GET方式

    單鏈接
            $url = 'https://tih-api.stb.gov.sg/content/v1/search/all';
            $result = file_get_contents($url);
            dump(json_decode($result,true));
    帶參數
            $url = 'https://tih-api.stb.gov.sg/content/v1/search/all';
            $params = array(
                'dataset' => "attractions",
                'language'=> "zh-cn",
                'apikey'=> "nHedovedovedoveG",
            );
            $url .= "?".http_build_query($params);
            $result = file_get_contents($url);
            dump(json_decode($result,true));
    以json格式提交
    $url = 'https://api-cn.faceplusplus.com/facepp/v3/detect';
    $dd = array(
        'api_key' => "xxxxxxxxxxxx",
        'api_secret'=> "xxxxxxxxxxxxxxx",
        'image_url'=> $upimg,
        'return_attributes'=>'smiling,age',
      );
    $result = $this->https_curl_json($url,$dd,'json');
    /* 發送json格式的數據,到api接口 */
        function https_curl_json($url,$data,$type){
            if($type=='json'){
                $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
                $data=json_encode($data);
            }
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
            $output = curl_exec($curl);
            if (curl_errno($curl)) {
                echo 'Errno'.curl_error($curl);//捕抓異常
            }
            curl_close($curl);
            return $output;
        }

     
  2. POST方式 
   $header = array(
            'Gateway-Authid:'.$appId,//申請的appId
            'Gateway-Request-Time:'. $time,//前請求時間,服務端會驗證該時間,需要±2min以內的日期
            'Gateway-Sign:'.$sign,
            'Gateway-Action-Method:shuyun.loyalty.member.register',//接口名稱
            'Gateway-Access-Token:'.$token,
        );
        //請求地址
        $sUrl = 'http://open-api.shuyun.com';
        //post數據
        $aData = array(
            'platCode'=>'WEIXIN',
            'name'=>$name, //微信名
            'mobile'=>$mobile, //電話
            'id'=>$mobile, //電話
            'shopId'=>'xxxxxxxxxxxx', //店鋪id,固定
        );
        $sResult = $this->httpRequest($sUrl, $header, $aData);
  function httpRequest($sUrl, $aHeader, $aData)
    {
        $ch = curl_init();
//        if(substr($sUrl,0,5)=='https'){
//            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
//            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 從證書中檢查SSL加密算法是否存在
//        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($aData));
        $sResult = curl_exec($ch);
        curl_close($ch);
        return $sResult;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章