登录  /  注册

关于PHP的curl功能扩展基本用法

藏色散人
发布: 2020-03-31 16:27:36
转载
1942人浏览过

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;
    }
}
登录后复制

本文系转载,原文地址是:

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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:tsmliyun网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号