用PHP封装一个强大且通用的CURL方法
用PHP封装一个强大且通用的CURL方法#
支持:get、post、put、delete、patch、options
/**
* @function 强大且通用的cURL请求库
* @param $url string 路径
* @param $method string 请求方式 如:get、post、put、delete、patch、options
* @param $payload array|string 荷载 如:['foo' => 'bar', 'upload_file' => new CURLFile(file_path)]或json{"foo":"bar"}
* @param $request_header array 请求头 如:['Content-Type' => 'json', 'Set-Cookie' => 'foo']
* @param $time_out int 超时秒数 如:10,(单位:秒)
* @return array [bool 请求是否成功, string 错误内容, [int http状态码, array 响应头, string 响应主体内容]];
*/
function curl($url, $method = 'GET', $payload = [], $request_header = [], $time_out = 10) {
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
$method = strtoupper($method);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'PUT') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'DELETE') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'PATCH') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'OPTIONS') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if($method == 'HEAD') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
} else {
curl_setopt($curl, CURLOPT_HTTPGET, true);
}
if ((parse_url($url)['scheme'] ?? '') == 'https') {
//防止对12306类似的使用自家的ssl证书,造成的请求失败
//禁止验证对等证书
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//禁止验证主机证书
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
if ($time_out) {
curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $time_out);
}
if ($request_header) {
//追加请求头 配置curl内容
curl_setopt($curl, CURLOPT_HTTPHEADER, array_map(function ($key, $value) {return $key . ': ' . $value;}, array_keys($request_header), $request_header));
}
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$response = curl_exec($curl);
$curl_info = curl_getinfo($curl);
$body = substr($response, $curl_info['header_size']);
$header_str = trim(substr($response, 0, $curl_info['header_size']));
$header = [];
if($header_str) {
$header_arr = explode("\r\n", $header_str);
foreach($header_arr as $every_header) {
$header_temp = explode(': ', $every_header, 2);
if(count($header_temp) == 2) {
$header[$header_temp[0]] = $header_temp[1];
}
}
}
curl_close($curl);
if (curl_errno($curl)) {
return ['status' => false, 'msg' => curl_error($curl), 'data' => ['http_code' => $curl_info['http_code'], 'body' => '', 'header' => [], 'info' => $curl_info]];
}
return ['status' => true, 'msg' => '', 'data' => ['http_code' => $curl_info['http_code'], 'body' => $body, 'header' => $header, 'info' => $curl_info]];
}
作者:Carver-大脸猫
出处:https://www.cnblogs.com/carver/articles/18735446
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请注明原处
本文来自博客园,作者:Carver-大脸猫,转载请注明原文链接:https://www.cnblogs.com/carver/articles/18735446
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现