wmSafe致力于互联网资源
道可道,非恒道,名可名,非恒名

【PHP相关】推荐几个自用的php curl封装的方法,支持自定义http(s)请求头,有需要自拿

本文最后更新于2022-10-21,已经有524 天没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!

前言

分享几种很好用的php进行url访问的curl封装方法,可以自定义请求头,十分友好,本人不长记这些工具类,因此放到博客以便自己实用,也可以分享给大家使用。
【PHP相关】推荐几个自用的php curl封装的方法,支持自定义http(s)请求头,有需要自拿

第一种:

适合请求第三方api获取json等等
function _curl($url,$post=0,$header=0,$cookie=0,$referer=0,$ua=0,$nobaody=0){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $httpheader[] = "Accept:*/*";
    $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if($post){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    if($header){
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
    }
    if($cookie){
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    if($referer){
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    }
    if($ua){
        curl_setopt($ch, CURLOPT_USERAGENT,$ua);
    }else{
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36');
    }
    if($nobaody){
        curl_setopt($ch, CURLOPT_NOBODY,1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}
//使用示例:
$a = _curl("https://blog.wm404.com/",false,true,false,'https://blog.wm404.com/',true,true);

第二种:

适合请求获取txt这类文本等等
$f='https://xxxxxxx.com/abc.txt';
$opts = [
    "http" => [
        "method" => "GET",
        "header" => 
            "Accept-language: zh\r\n" .
            "referer: https://blog.wm404.com/\r\n"
    ]
];

$context = stream_context_create($opts);
$file = file_get_contents($f, false, $context);

$arr = explode("\r\n", $file);//转换成数组
//去除值中的空格
foreach ($arr as &$row) {
    $row = trim($row);
}
unset($row);

$n=count($arr); //获得总行数
$rnd=rand(0,$n);    //产生随机行号
$rnd_line=$arr[$rnd]; //获得随机行

第三种:

比较全面的php curl封装类

<?php
/**
 * @author wmSafe
 * @link https://blog.wm404.com
 * @date 2020年11月12日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, "code" => $header
        ];
        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'
  ]
]);

携带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'
    ]
]);

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

echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
    'post'=>[        
        'user'=>123456,        
        'pwd'=>123
    ],    
    'GetCookie'=>1
]);
本原创文章未经允许不得转载 | 若要转载请注明出处,否则将承担相应的法律责任!
本文链接: https://blog.wm404.com/2022/04/26/b1e15a9e.html
赞赏排名 赞赏支持

评论

  • captcha
暂无评论,要不来一发?

您的关注就是我们最大的支持

联系我们 关于我们