PHP curl get post 请求的封装函数示例【get、post、put、delete等请求类型】(php写post接口)越早知道越好

随心笔谈12个月前发布 admin
103 0



//get请求
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); //设置超时时间:30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl检测
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 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求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请求
function delUrl($url, $header=[]) {
$ch=curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘DELETE’);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //为true,则会跟踪爬取重定向页面,否则,不会跟踪重定向页面
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间:30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl检测
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 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求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请求
function putUrl($url, $data=[], $header=[]) {
$ch=curl_init();
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘PUT’);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //为true,则会跟踪爬取重定向页面,否则,不会跟踪重定向页面
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间:30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl检测
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 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求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请求
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,则会跟踪爬取重定向页面,否则,不会跟踪重定向页面
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间:30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl检测
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 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求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 请求
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,则会跟踪爬取重定向页面,否则,不会跟踪重定向页面
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间:30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //忽略ssl检测
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 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求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;
}

可以在接口请求日志信息中记录运行时间,以便以后排查问题(程序执行缓慢,是哪个接口拖了时间)代码

$startTime=microtime(true);
for ($i=0; $i < 9999999; $i++) {
};
$endTime=microtime(true);
$runTime=sprintf(‘%.6f’, ($endTime-$startTime));
echo “执行时间为:{$runTime} s”;
die;

打印

执行时间为:0.202176 s

PS:针对常见的post、get、put、delete等请求方式,笔者经常使用postman或者ApiFox进行请求测试,并且通常前后端传输数据以json为主。 

您可能感兴趣的文章:PHP中使用cURL实现Get和Post请求的方法php中使用Curl、socket、file_get_contents三种方法POST提交数据php的curl实现get和post的代码PHP中的使用curl发送请求(GET请求和POST请求)PHP的curl实现get,post和cookie(实例介绍)详解php用curl调用接口方法,get和post两种方式php使用CURL模拟GET与POST向微信接口提交及获取数据的方法PHP CURL模拟GET及POST函数代码PHP如何使用cURL实现Get和Post请求PHP中使用CURL发送get/post请求上传图片批处理功能php curl发起get与post网络请求案例详解

© 版权声明

相关文章