封装PHP的curl请求支持POST和GET

代码:

<?php
function lwx_curl($url, $paras = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if (isset($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 (isset($paras['ctime'])) { // 连接超时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
    } else {
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    }
    if (isset($paras['rtime'])) { // 读取超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
    }
    if (isset($paras['post'])) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
    }
    if (isset($paras['header'])) {
        curl_setopt($ch, CURLOPT_HEADER, true);
    }
    if (isset($paras['cookie'])) {
        curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
    }
    if (isset($paras['refer'])) {
        if ($paras['refer'] == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
        }
    }
    if (isset($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 (isset($paras['nobody'])) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (isset($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 (isset($paras['loadurl'])) {
        $Headers = curl_getinfo($ch);
        if (isset($Headers['redirect_url'])) {
            $ret = $Headers['redirect_url'];
        } else {
            $ret = false;
        }
    }
    curl_close($ch);
    return $ret;
}

使用方法:

GET访问

echo lwx_curl("https://vlwx.com?url=qq.com");

POST访问

echo lwx_curl("https://vlwx.com/api.php",[
    'post'=>[
        'url'=>'qq.com'
    ]
]);

echo lwx_curl("https://vlwx.com/api.php",[
    'post'=>'url=qq.com'
]);

携带Cookie访问

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

模拟访问来源Refer

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

模拟UseaAgent

echo lwx_curl("https://vlwx.com/api.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 lwx_curl("https://vlwx.com/api.php?url=qq.com",[
    'post'=>[
        'file'=>new CURLFile(realpath("Curl.jpg"))
    ]
]);

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

获取301跳转地址

echo lwx_curl("https://url.cn/s/RNHSoek",[
    'loadurl'=>1
]);

查看返回Header信息

echo lwx_curl("https://vlwx.com/api.php?url=qq.com",[
    'header'=>1
]);

设置请求头信息

echo lwx_curl("https://vlwx.com/api.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'
    ]
]);

获取POST以后返回的Header、Body、Cookie内容

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

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

给TA打赏
共{{data.count}}人
人已打赏
源码分享

腾讯微云网盘直链解析ID版 PHP源码

2020-9-9 23:04:17

源码分享

Dplayer播放器替换、删除ts文件

2020-10-7 19:40:25

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
有新私信 私信列表
搜索