[PHP] PHP curl get post 请求的封装函数示例【get、post、put、del

1439 0
黑夜隐士 2023-5-13 12:42:27 | 显示全部楼层 |阅读模式
一、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为主。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行