网络编程
位置:首页>> 网络编程>> php编程>> PHP curl get post 请求的封装函数示例【get、post、put、delete等请求类型】

PHP curl get post 请求的封装函数示例【get、post、put、delete等请求类型】

作者:夏已微凉、  发布时间:2023-05-25 01:24:18 

标签:php,curl,post,get

一、get

//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

//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

//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

//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

//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为主。 

来源:https://blog.csdn.net/qq_36025814/article/details/116981144

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com