<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 前言 最近自己做了團隊內部的http調試工具,github開源地址 [https://github.com/diandianxiyu/ApiTesting](https://github.com/diandianxiyu/ApiTesting) ,通過這個項目又重新操作了PHP的curl函數,通過本篇博客進行記錄,和大家一起學習~ ### Code 本代碼通過PHP7環境運行。 ~~~ function curl($url, $method='GET',$fields = [], $headers=[],$auth = false){ //如果是get的獲取方式,拼接參數到url上 if($method == "GET"){ $fields_string = http_build_query($fields); $url=$url."?".$fields_string; } $curl = curl_init($url); //初始化 curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, $method ); //設定HTTP請求方式 curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_HEADER, 1); $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header[] = "Cache-Control: max-age=0"; $header[] = "Connection: keep-alive"; $header[] = "Keep-Alive: 300"; $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; // browsers keep this blank. curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($header,$headers)); //和參數中的header一起傳遞過去 if($auth){ curl_setopt($curl, CURLOPT_USERPWD, "$auth"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if($fields){ //POST if($method == "POST"){//單獨對POST方法設置參數傳遞 $fields_string = http_build_query($fields); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string); }else{ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ; curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ; } } $response = curl_exec($curl); //執行curl $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $header_string = substr($response, 0, $header_size); $body = substr($response, $header_size); $header_rows = explode(PHP_EOL, $header_string); foreach($header_rows as $key => $value){ $header_rows[$key]=trim($header_rows[$key]); } $i=0; foreach((array)$header_rows as $hr){ $colonpos = strpos($hr, ':'); $key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++; $headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr; } $j=0; foreach((array)$headers as $key => $val){ $vals = explode(';', $val); if(count($vals) >= 2){ unset($headers[$key]); foreach($vals as $vk => $vv){ $equalpos = strpos($vv, '='); $vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++; $headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv; } } } curl_close($curl); return array($body, $headers); //最終返回 result[0]為body,result[1]為header } ~~~ ### 參考資料 [http://www.php.net/curl](http://www.php.net/curl)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看