PHP curl get post 請(qǐng)求的封裝函數(shù)示例【get、post、put、delete等請(qǐng)求類型】
更新時(shí)間:2023年04月18日 09:02:50 作者:夏已微涼、
這篇文章主要介紹了PHP curl get post 請(qǐng)求的封裝函,包含了php使用curl針對(duì)get、post、put、delete等請(qǐng)求類型進(jìn)行封裝的操作技巧,以及CURLOPT_CUSTOMREQUEST控制DELETE、PUT請(qǐng)求類型的實(shí)現(xiàn)方法,需要的朋友可以參考下
一、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ì)跟蹤爬取重定向頁面,否則,不會(huì)跟蹤重定向頁面 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ì)跟蹤爬取重定向頁面,否則,不會(huì)跟蹤重定向頁面 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ì)跟蹤爬取重定向頁面,否則,不會(huì)跟蹤重定向頁面 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ì)跟蹤爬取重定向頁面,否則,不會(huì)跟蹤重定向頁面 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í)間,以便以后排查問題(程序執(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為主。
您可能感興趣的文章:
- PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實(shí)現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求)
- PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹)
- 詳解php用curl調(diào)用接口方法,get和post兩種方式
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求
- PHP中使用CURL發(fā)送get/post請(qǐng)求上傳圖片批處理功能
- php curl發(fā)起get與post網(wǎng)絡(luò)請(qǐng)求案例詳解
相關(guān)文章
PHP set_error_handler()函數(shù)使用詳解(示例)
本文詳細(xì)介紹PHP set_error_handler()函數(shù)的使用方法,最后還提供了一個(gè)實(shí)例2013-11-11國(guó)外PHP程序員的13個(gè)好習(xí)慣小結(jié)
我是一個(gè)PHP新手,只有6個(gè)月的PHP編程經(jīng)歷,并且是在一位經(jīng)過認(rèn)證的zend工程師的指導(dǎo)下完成工作的,每當(dāng)我編寫腳本時(shí),我會(huì)注意一些能讓我做得更好的細(xì)節(jié)2012-02-02php curl獲取網(wǎng)頁內(nèi)容(IPV6下超時(shí))的解決辦法
如果開啟了IPv6,curl默認(rèn)會(huì)優(yōu)先解析 IPv6,在對(duì)應(yīng)域名沒有 IPv6 的情況下,會(huì)等待 IPv6 dns解析失敗 timeout 之后才按以前的正常流程去找 IPv42013-07-07