一个封装的万能 PHP Curl 分享

前言

作者封装了很多功能,具体使用方法在下文,源码如果有更新,请前往作者仓库获取。

代码

<?php
/**
 * @author 百谷资源网
 * @link https://www.bigon.cn
 * @date 2023 年 08 月 27 日 18:00:30
 * @msg PHPCurl 封装的方法
 */
function teacher_curl($url, $paras = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if (@$paras['Header']) {
        $Header = $paras['Header'];
    } else {
        $Header[] = "Accept:*/*";
        $Header[] = "Accept-Encoding:gzip,deflate,sdch";
        $Header[] = "Accept-Language:zh-CN,zh;q=0.8";
        $Header[] = "Connection:close";
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
    if (@$paras['ctime']) { // 连接超时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
    } else {
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    }
    if (@$paras['rtime']) { // 读取超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
    }
    if (@$paras['post']) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
    }
    if (@$paras['header']) {
        curl_setopt($ch, CURLOPT_HEADER, true);
    }
    if (@$paras['cookie']) {
        curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
    }
    if (@$paras['refer']) {
        if ($paras['refer'] == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
        }
    }
    if (@$paras['ua']) {
        curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
    }
    if (@$paras['nobody']) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (@$paras['GetCookie']) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $result = curl_exec($ch);
        preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $headerSize); //状态码
        $body = substr($result, $headerSize);
        $ret = [
            "Cookie" => $matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
        ];
        curl_close($ch);
        return $ret;
    }
    $ret = curl_exec($ch);
    if (@$paras['loadurl']) {
        $Headers = curl_getinfo($ch);
        $ret = $Headers['redirect_url'];
    }
    curl_close($ch);
    return $ret;
}

使用方法

GET 访问

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com");

POST 访问

echo teacher_curl("https://api.oioweb.cn/api/beian.php",[
    'post'=>[
        'url'=>'qq.com'
    ]
]);

echo teacher_curl("https://api.oioweb.cn/api/beian.php",[
    'post'=>'url=qq.com'
]);

携带 Cookie 访问

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'cookie'=>'cookie 内容'
]);

模拟访问来源 Refer

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'refer'=>'https://api.oioweb.cn'
]);

模拟 UseaAgent

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'ua'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
]);

文件上传

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'post'=>[
        'file'=>new CURLFile(realpath("Curl.jpg"))
    ]
]);

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'post'=>new CURLFile(realpath("Curl.jpg"))
]);

获取 301 跳转地址

echo teacher_curl("https://mmbizurl.cn/s/RNHSo6Dek",[
    'loadurl'=>1
]);

查看返回 Header 信息

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'header'=>1
]);

设置请求头信息

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'Header'=>[
        'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0'
    ]
]);

获取请求的全部信息

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[
    'post'=>[
        'user'=>123456,
        'pwd'=>123
    ],
    'GetCookie'=>1
]);

最后

还有其他的功能,自己研究吧!还是很强大的!

温馨提示:若内容/图片/下载链接失效,请在下方留言或登录后私信站长
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容