国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP 傳輸會話curl函數(shù)的實例詳解

2024-09-04 11:49:26
字體:
供稿:網(wǎng)友

PHP 傳輸會話curl函數(shù)的實例詳解

前言:接手公司項目PC端負責(zé)人的重擔(dān),責(zé)任擔(dān)當(dāng)重大;從需求分析,畫流程圖,建表,編碼,測試修bug,上線維護等我一個光桿司令一人完成(當(dāng)然還有一個技術(shù)不錯的前端配合,感謝主管的幫助),雖然累點加班多點但感覺還行吧,公司都是一個鳥樣。

閑話不多說了,因為項目中經(jīng)常需要調(diào)取java那邊的接口,既然涉及到請求接口那就有了http的請求方式,PHP常見的是GET/POST兩種當(dāng)然還有其他的比如put等,java那邊經(jīng)常用到GET/POST/PUT/DELETE等方式,請求接口當(dāng)然要用到curl的相關(guān)函數(shù)了,都是看文檔調(diào)試的希望大家都看文檔,下面是我封裝好的相關(guān)函數(shù)等(大概總結(jié)下,已調(diào)通):

示例代碼:

  1. private $serverhost = "https://demo.xxx.cn"; //測試 
  2.     /** 
  3.      * 請求接口封裝  get/post/put/delete等 
  4.      * access public 
  5.      * @param string $url 接口地址 
  6.      * @param string $params 參數(shù) 
  7.      * @param string $type 類型 get/post/put/delete 
  8.      * @return bool/array 
  9.      */ 
  10.      public function getcurldata($url,$params,$type="get"){ 
  11.         $url = $this->serverhost.$url
  12.    
  13.         $response = array(); 
  14.         if($type == 'get'){ //get請求 
  15.           //請求頭可以加其他設(shè)置 
  16.           $headers = array
  17.               'Content-type: application/json;charset=UTF-8'
  18.           ); 
  19.           $ch = curl_init(); 
  20.           curl_setopt($ch, CURLOPT_URL, $url); 
  21.           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  22.           curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
  23.           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
  24.           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  25.           $response = curl_exec($ch); 
  26.    
  27.        }elseif ($type == 'post'){  //post請求 
  28.    
  29.          $headers = array
  30.             'Content-type: application/json;charset=UTF-8'
  31.          ); 
  32.          $ch = curl_init(); 
  33.          curl_setopt($ch, CURLOPT_URL, $url); 
  34.          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  35.          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
  36.          curl_setopt($ch, CURLOPT_POST, true);  //注意這幾行 
  37.          curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //注意這幾行 
  38.          //curl_setopt($ch, CURLOPT_HEADER, true); 
  39.          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
  40.          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  41.          $response = curl_exec($ch); 
  42.    
  43.        }elseif ($type == 'put'){ //put請求 
  44.    
  45.           $headers = array
  46.                'Content-type: application/json;charset=UTF-8'
  47.           ); 
  48.    
  49.           $ch = curl_init(); 
  50.           curl_setopt($ch, CURLOPT_URL, $url); 
  51.           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  52.           curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
  53.           curl_setopt($ch, CURLOPT_PUT, true); //注意這幾行 
  54.           curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
  55.           //curl_setopt($ch, CURLOPT_HEADER, true); 
  56.           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
  57.           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  58.           $response = curl_exec($ch); 
  59.        } 
  60.    
  61.        return $response
  62.     }  
  63.  //如何調(diào)用上面代碼 
  64.    //get方式 
  65.    /** 
  66.     * 查詢我創(chuàng)建過的班級 
  67.     * @param string $url 接口地址 
  68.     * @param string $params 參數(shù) 
  69.     * @param string $type 類型 get 
  70.     * @return array 
  71.    */ 
  72.     public function mycreateclass($userid){ 
  73.    
  74.        $url = "/xxx/xxxx/xxxx/".$userid//請求地址拼接 
  75.        $response = $this->getcurldata($url,array(),"get"); 
  76.        $createdclass = json_decode($response, true); //返回json格式數(shù)據(jù) 
  77.    
  78.        return $createdclass
  79.     } 
  80.     /** post方式請求 
  81.      * 用戶登錄判斷 
  82.      * access public 
  83.      * @param string $username 用戶名 
  84.      * @param string $password 密碼 
  85.      * @return bool 
  86.     */ 
  87.     public function getlogin($username,$password
  88.     { 
  89.        //要post的數(shù)據(jù) 
  90.        $params = array
  91.           "username"   => $username
  92.           "password"   => $password 
  93.        ); 
  94.       $params = json_encode($params, 64|256); 
  95.       $uri = "/xxx/xxx/login"
  96.       $response = $this->getcurldata($uri,$params,"post"); 
  97.       $result = json_decode($response, true); 
  98.    
  99.       return $result ; 
  100.     } 
  101.        
  102.      /*身份轉(zhuǎn)換--put 請求 
  103.       */ 
  104.      public function changeuserole($token){ 
  105.          //要put的數(shù)據(jù) 
  106.         $params = array(); 
  107.         $params = json_encode($params, 64|256); 
  108.    
  109.         $uri = "/xxx/xxx/xxx/".$token."/"
  110.         $response = $this->getcurldata($uri,$params,"put"); 
  111.         $result = json_decode($response, true); 
  112.    
  113.         //dump($result);die; 
  114.    
  115.         return $result
  116.      } 
  117.  //還有一個delete方式 大家自己參考文檔調(diào)試下吧 
  118. 上面3個請求方式都是單次請求(即請求一次)*************** 
  119. PHP自帶函數(shù)curl_multi_get_contents函數(shù)(thinkphp自帶此函數(shù),可以微調(diào)下): 
  120.      /**  
  121.       * 批量發(fā)起請求 已調(diào)通 
  122.       * curl multi POST數(shù)據(jù) 
  123.       * */ 
  124.      public function curl_multi_get_contents($url=array(), $param = array(), $timeout=1000){ 
  125.          $userAgent = 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)'
  126.          $headers = array
  127.             'Content-type: application/json;charset=UTF-8'
  128.          ); 
  129.          $curl_array=array(); 
  130.          $mh = curl_multi_init(); 
  131.          foreach($url as $uk=>$uv){ 
  132.             $curl_array[$uk] = curl_init(); 
  133.          } 
  134.          unset($uk,$uv); 
  135.          foreach($url as $uk=>$uv) { 
  136.              $options = array
  137.                 CURLOPT_URL   => $uv
  138.                 CURLOPT_TIMEOUT => $timeout
  139.                 CURLOPT_RETURNTRANSFER => true, 
  140.                 CURLOPT_DNS_CACHE_TIMEOUT => 86400, 
  141.                 CURLOPT_DNS_USE_GLOBAL_CACHE  => true, 
  142.                 CURLOPT_SSL_VERIFYPEER => 0, 
  143.                 CURLOPT_HEADER => false, 
  144.                 CURLOPT_USERAGENT  => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  145.                 CURLOPT_HTTPHEADER => $headers
  146.              ); 
  147.          if (isset($param[$uk])) { 
  148.              foreach ($param[$ukas $_k => $_v) { 
  149.                 //$options[$_k] = $_v; 
  150.                 $optionsparam[$_k] = $_v
  151.                 $options[CURLOPT_POSTFIELDS] = json_encode($optionsparam, 64|256); 
  152.              } 
  153.           } 
  154.    
  155.          curl_setopt_array($curl_array[$uk], $options); 
  156.          curl_multi_add_handle($mh$curl_array[$uk]); 
  157.          unset($options); 
  158.       } 
  159.       unset($uk,$uv); 
  160.       $running = NULL; 
  161.       do { 
  162.            curl_multi_exec($mh,$running); 
  163.        } while($running > 0); 
  164.    
  165.        $res = array(); 
  166.        foreach($url as $uk=>$uv){ 
  167.             $res[$uk] = curl_multi_getcontent($curl_array[$uk]); 
  168.        } 
  169.        unset($uk,$uv); 
  170.        foreach($url as $uk=>$uv){ 
  171.            curl_multi_remove_handle($mh$curl_array[$uk]); 
  172.        } 
  173.       unset($url,$curl_array,$uk,$uv); 
  174.       curl_multi_close($mh); 
  175.       return $res
  176.    } 
  177.  //如何調(diào)用--批量發(fā)起請求 
  178.     //批量請求加入班級 
  179.     public function batchjoinclass($token,$batchjoinclass){ 
  180.         $urlarr = $param = $returndata = array(); 
  181.    
  182.         $param = $batchjoinclass//二維數(shù)組 格式如下 
  183.    
  184.         /* 
  185.          $param[1]['name'] = '班級新1'; 
  186.          $param[1]['iamge'] = 'xxx11.png'; 
  187.          $param[1]['iamgeType'] = 'CUSTOM'; 
  188.          $param[2]['name'] = '班級新2'; 
  189.          $param[2]['iamge'] = 'xxx.png'; 
  190.          $param[2]['iamgeType'] = 'CUSTOM'; 
  191.        */ 
  192.    
  193.        //獲取請求url 
  194.        foreach($batchjoinclass as $key=>$val){ 
  195.            $urlarr[$key] = $this->serverhost."/xxx/xxxx/xxxx/".$token
  196.         } //Vevb.com 
  197.    
  198.         $res = $this->curl_multi_get_contents($urlarr,$param); 
  199.    
  200.         //<a href="http://www.111cn.net/zhuanti/geshihua/" class="anchor" target="_blank">格式化</a>返回數(shù)據(jù) 
  201.         foreach($res as $key=>$val){ 
  202.             $returndata[$key] = json_decode($val,true); 
  203.         } 
  204.    
  205.         return $returndata
  206.     } 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 福贡县| 赣州市| 梁山县| 曲阳县| 光山县| 南开区| 会宁县| 上林县| 深州市| 邹平县| 资溪县| 清水县| 洛宁县| 达州市| 鹰潭市| 宿迁市| 浪卡子县| 姜堰市| 威信县| 万山特区| 沧州市| 清水县| 洛南县| 合川市| 吴堡县| 辉南县| 绥化市| 奇台县| 田东县| 赞皇县| 西林县| 承德县| 克拉玛依市| 哈巴河县| 师宗县| 涟源市| 沅江市| 闵行区| 博乐市| 肃宁县| 资溪县|