文章詳情頁(yè)
PHP curl get post 請(qǐng)求的封裝函數(shù)示例【get、post、put、delete等請(qǐng)求類型】
瀏覽:231日期: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í)間,以便以后排查問題(程序執(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. jQuery+PHP實(shí)現(xiàn)圖片上傳并提交功能2. PHP讀取和寫入CSV文件的示例代碼3. 24個(gè)有用的PHP類庫(kù)分享4. 在線競(jìng)拍系統(tǒng)的PHP實(shí)現(xiàn)框架(二)5. 為什么相對(duì)PHP黑python的更少6. ThinkPHP5中如何使用redis7. 為PHP模塊添加SQL SERVER2012數(shù)據(jù)庫(kù)的步驟詳解8. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)9. PHP基礎(chǔ)之流程控制10——goto語(yǔ)句10. PHP網(wǎng)絡(luò)處理模塊FPM源碼分析
排行榜

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