文章詳情頁(yè)
PHP curl get post 請(qǐng)求的封裝函數(shù)示例【get、post、put、delete等請(qǐng)求類(lèi)型】
瀏覽:227日期: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 開(kāi)始可用。這個(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 開(kāi)始可用。這個(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 開(kāi)始可用。這個(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 開(kāi)始可用。這個(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 開(kāi)始可用。這個(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ì)常見(jiàn)的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. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對(duì)象2. 編寫(xiě)自己的php擴(kuò)展函數(shù)3. 手動(dòng)架設(shè)PHP本地測(cè)試服務(wù)器4. PHP新手告訴你應(yīng)該如何學(xué)習(xí)編程5. PHP JSAPI調(diào)支付API實(shí)現(xiàn)微信支付功能詳解6. PHP5.3閉包特性及應(yīng)用詳解7. 25個(gè)PHP游戲編程腳本代碼8. 精通PHP的十大要點(diǎn)9. PHP基礎(chǔ)之流程控制2——流程控制的替代語(yǔ)法10. PHP幾個(gè)常用的去空、分組、調(diào)試數(shù)組函數(shù)
排行榜

網(wǎng)公網(wǎng)安備