您的当前位置:首页>全部文章>文章详情

php使用cURL请求封装

发表于:2024-04-23 22:08:22浏览:154次TAG: #curl #PHP #请求 #post #get

引言

使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了。

示例


// get请求
$url = "http://xxx.xxx.com/api/article/search";
$data = ['kw'=>'李四'];
$res = \app\common\utils\Curl::get($url,$data,['timeout'=>3]);

// post请求
$url = "http://xxx.xxx.com/api/article/save";
$data = ['title'=>'标题','file'=>'/path/to/your/file.txt'];
$res = \app\common\utils\Curl::post($url,$data,['timeout'=>5]);

<?php
namespace app\common\utils;
/**
 * 请求
 * Class Curl
 * @package app\common\utils
 */
class Curl {
    /**
     * get请求
     * @param $url
     * @param $data
     * @param array $options
     * @return array
     */
    public static function get($url,$data,$options = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data));
        // 设置超时,单位秒,示例:3
        if (!empty($options['timeout'])) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
        }
        // 头部,示例:array('Content-Type: text/plain')
        if (!empty($options['httpheader'])) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $options['httpheader']);
        }
        // 用户代理,示例:true
        if (!empty($options['useragent'])) {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
        }
        // 设置代理,示例:proxy.example.com:8080
        if (!empty($options['proxy'])) {
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
            curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
        }
        // 使用COOKIE,示例:username=John Doe; usertype=member
        if (!empty($options['cookie'])) {
            curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
        }
        // 进行HTTP认证,示例:username:password
        if (!empty($options['userpwd'])) {
            curl_setopt($ch, CURLOPT_USERPWD, $options['userpwd']);
        }
        // 开启ssl
        if (!empty($options['ssl'])) {
            // SSL证书文件,示例:/path/to/your/certificate.crt
            if (!empty($options['ssl_cert'])) {
                curl_setopt($ch, CURLOPT_SSLCERT, $options['ssl_cert']);
            }
            // 指SSL证书的私钥,示例:/path/to/your/private_key.pem
            if (!empty($options['ssl_key'])) {
                curl_setopt($ch, CURLOPT_SSLKEY, $options['ssl_key']);
            }
            // 设置为false,关闭SSL证书验证,以禁用对远程服务器的证书验证。由于您使用的是自签名证书,因此服务器证书将无法通过正常的验证过程。
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            // 设置为false,以禁用对远程服务器主机名的验证。
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        // 设置为true,以便将响应作为字符串返回,而不是直接输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error) {
            return ['status'=>0,'msg'=>$error,'data'=>null];
        }
        else {
            return ['status'=>1,'msg'=>'success','data'=>$result];
        }
    }
    /**
     * post请求
     * @param $url
     * @param $data
     * @param array $options
     * @return array
     */
    public static function post($url,$data,$options = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        // 文件上传,示例:/path/to/your/file.txt
        if (!empty($options['file'])) {
            $field = $options['file_field'] ?? 'file';
            $file = curl_file_create($options['file']);
            $data = [$field=>$file];
        }
        // 参数,示例:
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        // 设置超时,单位秒,示例:3
        if (!empty($options['timeout'])) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
        }
        // 头部,示例:array('Content-Type: text/plain')
        if (!empty($options['httpheader'])) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $options['httpheader']);
        }
        // 用户代理,示例:true
        if (!empty($options['useragent'])) {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
        }
        // 设置代理,示例:proxy.example.com:8080
        if (!empty($options['proxy'])) {
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
            curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
        }
        // 使用COOKIE,示例:username=John Doe; usertype=member
        if (!empty($options['cookie'])) {
            curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
        }
        // 进行HTTP认证,示例:username:password
        if (!empty($options['userpwd'])) {
            curl_setopt($ch, CURLOPT_USERPWD, $options['userpwd']);
        }
        // 开启ssl
        if (!empty($options['ssl'])) {
            // SSL证书文件,示例:/path/to/your/certificate.crt
            if (!empty($options['ssl_cert'])) {
                curl_setopt($ch, CURLOPT_SSLCERT, $options['ssl_cert']);
            }
            // 指SSL证书的私钥,示例:/path/to/your/private_key.pem
            if (!empty($options['ssl_key'])) {
                curl_setopt($ch, CURLOPT_SSLKEY, $options['ssl_key']);
            }
            // 设置为false,关闭SSL证书验证,以禁用对远程服务器的证书验证。由于您使用的是自签名证书,因此服务器证书将无法通过正常的验证过程。
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            // 设置为false,以禁用对远程服务器主机名的验证。
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        // 设置为true,以便将响应作为字符串返回,而不是直接输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error) {
            return ['status'=>0,'msg'=>$error,'data'=>null];
        }
        else {
            return ['status'=>1,'msg'=>'success','data'=>$result];
        }
    }
}