文章詳情頁(yè)
PHP curl get post 請(qǐng)求的封裝函數(shù)示例【get、post、put、delete等請(qǐng)求類型】
瀏覽:150日期:2022-06-10 16:51:11
一、get
//get請(qǐng)求 function getUrl($url, $header = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPGET, true); if ($header) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時(shí)時(shí)間:30s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl檢測(cè) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 或 TRUE 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。- curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 時(shí)追蹤句柄的請(qǐng)求字符串,從 PHP 5.1.3 開始可用。這個(gè)很關(guān)鍵,就是允許你查看請(qǐng)求header $output = curl_exec($ch); if (!$output) { //echo "request $url fail:", (array)curl_error($ch); //記錄日志 } curl_close($ch); // echo "request $url success:" . json_encode(array($url, $header, $output), true); //記錄日志 return $output; }
二、del
//del請(qǐng)求 function delUrl($url, $header = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //為true,則會(huì)跟蹤爬取重定向頁(yè)面,否則,不會(huì)跟蹤重定向頁(yè)面 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時(shí)時(shí)間:30s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl檢測(cè) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 或 TRUE 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。- curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 時(shí)追蹤句柄的請(qǐng)求字符串,從 PHP 5.1.3 開始可用。這個(gè)很關(guān)鍵,就是允許你查看請(qǐng)求header curl_setopt($ch, CURLOPT_URL, $url); $output = curl_exec($ch); if (!$output) { //echo "request $url fail:", (array)curl_error($ch); //記錄日志 } curl_close($ch); // echo "request $url success:" . json_encode(array($url, $header, $output), true); //記錄日志 return $output; }
三、put
//put請(qǐng)求 function putUrl($url, $data = [], $header = []) { $ch = curl_init(); if (!empty($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定義提交的數(shù)據(jù) } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //為true,則會(huì)跟蹤爬取重定向頁(yè)面,否則,不會(huì)跟蹤重定向頁(yè)面 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時(shí)時(shí)間:30s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl檢測(cè) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 或 TRUE 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。- curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 時(shí)追蹤句柄的請(qǐng)求字符串,從 PHP 5.1.3 開始可用。這個(gè)很關(guān)鍵,就是允許你查看請(qǐng)求header curl_setopt($ch, CURLOPT_URL, $url); $output = curl_exec($ch); if (!$output) { //echo "request $url fail:", (array)curl_error($ch); //記錄日志 } curl_close($ch); // echo "request $url success:" . json_encode(array($url, $header, $output), true); //記錄日志 return $output; }
四、post
//post請(qǐng)求 function postUrl($url, $data, $header = []) { $ch = curl_init(); if (!empty($data)) { curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //為true,則會(huì)跟蹤爬取重定向頁(yè)面,否則,不會(huì)跟蹤重定向頁(yè)面 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時(shí)時(shí)間:30s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl檢測(cè) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 或 TRUE 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。- curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 時(shí)追蹤句柄的請(qǐng)求字符串,從 PHP 5.1.3 開始可用。這個(gè)很關(guān)鍵,就是允許你查看請(qǐng)求header curl_setopt($ch, CURLOPT_URL, $url); $output = curl_exec($ch); if (!$output) { //echo "request $url fail:", (array)curl_error($ch); //記錄日志 } curl_close($ch); // echo "request $url success:" . json_encode(array($url, $header, $output), true); //記錄日志 return $output; }
五、post json
//post json 請(qǐng)求 function postJsonUrl($url, $data, $header = []) { $data = json_encode($data); $ch = curl_init(); if (!empty($data)) { curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $header[]="Content-Type: application/json; charset=utf-8"; curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //為true,則會(huì)跟蹤爬取重定向頁(yè)面,否則,不會(huì)跟蹤重定向頁(yè)面 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時(shí)時(shí)間:30s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl檢測(cè) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 或 TRUE 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。- curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 時(shí)追蹤句柄的請(qǐng)求字符串,從 PHP 5.1.3 開始可用。這個(gè)很關(guān)鍵,就是允許你查看請(qǐng)求header curl_setopt($ch, CURLOPT_URL, $url); $output = curl_exec($ch); if (!$output) { //echo "request $url fail:", (array)curl_error($ch); //記錄日志 } curl_close($ch); // echo "request $url success:" . json_encode(array($url, $header, $output), true); //記錄日志 return $output; }
六、計(jì)算請(qǐng)求運(yùn)行時(shí)間
- 可以在接口請(qǐng)求日志信息中記錄運(yùn)行時(shí)間,以便以后排查問(wèn)題(程序執(zhí)行緩慢,是哪個(gè)接口拖了時(shí)間)
- 代碼
$startTime = microtime(true); for ($i = 0; $i < 9999999; $i++) { }; $endTime = microtime(true); $runTime = sprintf("%.6f", ($endTime-$startTime)); echo "執(zhí)行時(shí)間為:{$runTime} s"; die;
- 打印
執(zhí)行時(shí)間為:0.202176 s
PS:針對(duì)常見的post、get、put、delete等請(qǐng)求方式,筆者經(jīng)常使用postman或者ApiFox進(jìn)行請(qǐng)求測(cè)試,并且通常前后端傳輸數(shù)據(jù)以json為主。
標(biāo)簽:
PHP
上一條:使用Canal實(shí)現(xiàn)PHP應(yīng)用程序與MySQL數(shù)據(jù)庫(kù)的實(shí)時(shí)數(shù)據(jù)同步下一條:IIS+PHP添加對(duì)webp格式圖像的支持配置方法
相關(guān)文章:
1. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查2. PHP擴(kuò)展之針對(duì)搜索引擎的擴(kuò)展(二)—— Sphinx簡(jiǎn)介、安裝及使用3. php設(shè)計(jì)模式之策略模式實(shí)例分析【星際爭(zhēng)霸游戲案例】4. PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析5. php中unable to fork報(bào)錯(cuò)簡(jiǎn)單解決方法6. php基礎(chǔ)字符串與數(shù)組知識(shí)點(diǎn)講解7. PHP結(jié)構(gòu)型模式之裝飾器模式8. PHP 與 Perl 語(yǔ)言對(duì)比9. PHP結(jié)構(gòu)型模式之享元模式詳解10. nginx簡(jiǎn)單配置多個(gè)php服務(wù)實(shí)例教程
排行榜
