php的curl功能提供了很多函数,需要将这些函数按特定的步骤组合到一起,我们先来了解下php建立curl请求的基本步骤。
$ch = curl_init(); // 创建一个新的CURL资源赋给变量$ch curl_setopt($ch, CURLOPT_URL, $url); // 设置URL $response = curl_exec($ch); // 执行,获取URL并输出到浏览器 curl_close($ch); // 释放资源
如果我们希望获取内容但不输出,可以使用 CURLOPT_RETURNTRANSFER 参数,并设置其值为非0或者true值。
代码如下:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
我们可以通设置函数curl_setopt()的不同参数,可以获得不同的结果,这也是CURL扩展的强大之处。curl_setopt()函数的常用参数选项具体可查阅官方文档,此处就不列举。
下面是我常用的curl get和post请求的方法:
立即学习“PHP免费学习笔记(深入)”;
get请求:
public function httpGet(string $url = '')
    {
        // 记录请求信息的日志
        // todo
        
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            //https 请求
            if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            }
            $response = curl_exec($ch);
            $errorCode = curl_errno($ch);
            curl_close($ch);
            if (!empty($errorCode)) {
                // 可记录错误码日志
                return null;
            }
            // 记录返回结果日志
            return $response;
        } catch (\Exception $e) {
            $errorLog = [
                'msg' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
                'data' => [
                    'url' => $url,
                ]
            ];
            // 记录错误日志
            return null;
        }
    }
```phpPOST请求:
public function httpPost(string $url = '', array $data = [])
{
        // 记录请求信息的日志
        // todo
    try {
        $jsonData = json_encode($data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($jsonData)
        ]);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($curl);
        $errorCode = curl_errno($curl);
        curl_close($curl);
        if (!empty($errorCode)) {
            // 可记录错误码日志
            return null;
        }
        // 记录返回结果日志
        return json_decode($result, true);
    } catch (\Exception $e) {
        $errorData = [
            'msg' => $e->getMessage(),
            'trace' => $e->getTraceAsString(),
            'data' => [
                'url' => $url,
                'postData' => $data
            ]
        ];
        // 记录错误日志
        return null;
    }
}本文系转载,原文地址是:https://tsmliyun.github.io/php/PHP%E7%9A%84CURL%E5%8A%9F%E8%83%BD%E6%89%A9%E5%B1%95%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/
推荐:PHP视频教程
以上就是关于PHP的curl功能扩展基本用法的详细内容,更多请关注php中文网其它相关文章!
                        
                        PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号