温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP的curl功能扩展如何使用

发布时间:2020-04-01 16:03:13 来源:亿速云 阅读:82 作者:小新 栏目:编程语言

今天小编给大家分享的是PHP的curl功能扩展如何使用,很多人都不太了解,今天小编为了让大家更加了解PHP的curl功能扩展,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

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请求的方法:

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;
        }
    }
```php

POST请求:

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;
    }
}

关于PHP的curl功能扩展如何使用就分享到这里了,当然并不止以上和大家分析的办法,不过小编可以保证其准确性是绝对没问题的。希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI