U.S. flag

咔咔技术站所属网站

Dot gov

所属网站使用 KKJSZ.CN
KKJSZ.CN 属于咔咔技术站及其附属网站使用


HTTPS

KKJSZ.CN域名全面使用 HTTPS
小锁 ( ) 或 https:// 表示您已安全连接到KKJSZ.CN网站。

一个万能的PHP_CURL函数

咔咔云
2023-01-19 / 0 评论 / 200 阅读 / 正在检测是否收录...

PHP_curl函数

CURL是PHP的一个扩展,利用该扩展可以实现服务器之间的数据或文件传输,用来采集网络中的html网页文件、其他服务器提供接口数据等。
PHP_curl函数

使用场景

使用准备

万能curl函数

PHP_curl函数网上有很多 ,但大多是需要自行匹配的,所以咔咔云站长今天给大家分享一个万能curl函数,同时也会给大家分享下咔咔云自用的curl函数。

<?php
/**
 * @author 咔咔云
 * @link https://www.kkjsz.cn
 * @date 2022年6月14日10:16:06
 * @msg PHPCurl封装的方法
 */
function teacher_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://m.qzone.com/infocenter?g_f=');
        } 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;
}

使用方法

1.get访问

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

2.post访问

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

或者是

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

3.携带Cookie访问

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

4.模拟访问来源Refer

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

5.模拟UseaAgent

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

6.文件上传

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

或者是

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

7.获取301跳转地址

echo teacher_curl("https://t.cn/A6XQ4phv",[
    'loadurl'=>1
]);

8.查看返回Header信息

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

9.设置请求头信息

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

10.获取请求的全部信息

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

咔咔云站长自用PHP_curl函数

function curl($url){ //Curl GET
    $ch = curl_init();     // Curl 初始化
    $timeout = 30;     // 超时时间:30s
    $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';// 伪造抓取 UA
    $ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
    curl_setopt($ch, CURLOPT_URL, $url);// 设置 Curl 目标
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Curl 请求有返回的值
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// 设置抓取超时时间
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// 跟踪重定向
    curl_setopt($ch, CURLOPT_REFERER, 'https://www.baidu.com/');//模拟来路
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //伪造IP
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);// 伪造ua
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//强制协议为1.0
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//强制使用IPV4协议解析域名
    $content = curl_exec($ch);
    curl_close($ch);// 结束 Curl
    return $content;// 函数返回内容
}

本文首发于:https://www.kkjsz.cn/archives/162.html

本文共 381 个字数,平均阅读时长 ≈ 1分钟
0

打赏

海报

正在生成.....

评论 (0)

取消