切换风格

默认晚霞 雪山 粉色心情 伦敦 花卉 绿野仙踪 加州 白云 星空 薰衣草 城市 简约黑色 简约米色 龙珠
回复 0

50

主题

76

帖子

1188

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1188
php带cookie访问下载文件 header设置[复制链接]
发表于 2024-4-26 10:51:57 | 显示全部楼层 |阅读模式
phpCurl函数类,网上很多,这里分享一个万能phpcurl,包含phpcurl函数类模拟Curl get post header refer携带Cookie模拟访问来源Refer模拟UseaAgent
  1. <?php
  2. /**
  3. * @author 教书先生
  4. * @link https://blog.oioweb.cn
  5. * @date 2021年6月13日10:29:04
  6. * @msg PHPCurl封装的方法
  7. */
  8. function teacher_curl($url, $paras = [])
  9. {
  10.     $ch = curl_init();
  11.     curl_setopt($ch, CURLOPT_URL, $url);
  12.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  13.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  14.     if (isset($paras['Header'])) {
  15.         $Header = $paras['Header'];
  16.     } else {
  17.         $Header[] = "Accept:*/*";
  18.         $Header[] = "Accept-Encoding:gzip,deflate,sdch";
  19.         $Header[] = "Accept-Language:zh-CN,zh;q=0.8";
  20.         $Header[] = "Connection:close";
  21.     }
  22.     curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
  23.     if (isset($paras['ctime'])) { // 连接超时
  24.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
  25.     } else {
  26.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  27.     }
  28.     if (isset($paras['rtime'])) { // 读取超时
  29.         curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
  30.     }
  31.     if (isset($paras['post'])) {
  32.         curl_setopt($ch, CURLOPT_POST, 1);
  33.         curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
  34.     }
  35.     if (isset($paras['header'])) {
  36.         curl_setopt($ch, CURLOPT_HEADER, true);
  37.     }
  38.     if (isset($paras['cookie'])) {
  39.         curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
  40.     }
  41.     if (isset($paras['refer'])) {
  42.         if ($paras['refer'] == 1) {
  43.             curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
  44.         } else {
  45.             curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
  46.         }
  47.     }
  48.     if (isset($paras['ua'])) {
  49.         curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
  50.     } else {
  51.         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");
  52.     }
  53.     if (isset($paras['nobody'])) {
  54.         curl_setopt($ch, CURLOPT_NOBODY, 1);
  55.     }
  56.     curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  57.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  58.     if (isset($paras['GetCookie'])) {
  59.         curl_setopt($ch, CURLOPT_HEADER, 1);
  60.         $result = curl_exec($ch);
  61.         preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
  62.         $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  63.         $header = substr($result, 0, $headerSize); //状态码
  64.         $body = substr($result, $headerSize);
  65.         $ret = [
  66.             "Cookie" => $matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  67.         ];
  68.         curl_close($ch);
  69.         return $ret;
  70.     }
  71.     $ret = curl_exec($ch);
  72.     if (isset($paras['loadurl'])) {
  73.         $Headers = curl_getinfo($ch);
  74.         if (isset($Headers['redirect_url'])) {
  75.             $ret = $Headers['redirect_url'];
  76.         } else {
  77.             $ret = false;
  78.         }
  79.     }
  80.     curl_close($ch);
  81.     return $ret;
  82. }
复制代码
使用方法
GET访问

  1. echo teacher_curl("https://kuaidi.jsonin.com/JT5124806639322.html");
复制代码
POST访问
  1. echo teacher_curl("https://kuaidi.ipip5.com",[
  2.     'post'=>[
  3.         'dh'=>'JT5124806639322'
  4.     ]
  5. ]);
复制代码

  1. echo teacher_curl("https://kuaidi.jsonin.com/",[
  2.     'post'=>'dh=JT5124806639322'
  3. ]);
复制代码
携带Cookie访问
  1. echo teacher_curl("https://www.xiaodoubi.com/",[
  2.     'cookie'=>'cookie内容'
  3. ]);
复制代码
模拟访问来源Refer
  1. echo teacher_curl("https://www.xiaodoubi.com/",[
  2.     'refer'=>'https://www.xiaof.com'
  3. ]);
复制代码
模拟UseaAgent
  1. echo teacher_curl("https://www.xiaodoubi.com/",[
  2.     'ua'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
  3. ]);
复制代码
文件上传
  1. echo teacher_curl("https://www.ipip5.com/",[
  2.     'post'=>[
  3.         'file'=>new CURLFile(realpath("Curl.jpg"))
  4.     ]
  5. ]);
复制代码

  1. echo teacher_curl("https://www.ipip5.com/",[
  2.     'post'=>new CURLFile(realpath("Curl.jpg"))
  3. ]);
复制代码
获取301跳转地址
  1. echo teacher_curl("https://www.xiaof.com",[
  2.     'loadurl'=>1
  3. ]);
复制代码
设置请求头信息
  1. echo teacher_curl("https://www.ipip5.com/",[
  2.     'Header'=>[
  3.         'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
  4. accept-encoding: gzip, deflate, br
  5. accept-language: zh-CN,zh;q=0.9
  6. cache-control: max-age=0'
  7.     ]
  8. ]);
复制代码
获取请求的全部信息
  1. echo teacher_curl("https://www.ipip5.com/today/api.php?type=json",[
  2.     'post'=>[
  3.         'user'=>123456,
  4.         'pwd'=>123
  5.     ],
  6.     'GetCookie'=>1
  7. ]);
复制代码
我自用的
  1. function curl($url){ //Curl GET
  2.     $ch = curl_init();     // Curl 初始化
  3.     $timeout = 30;     // 超时时间:30s
  4.     $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';// 伪造抓取 UA
  5.     $ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
  6.     curl_setopt($ch, CURLOPT_URL, $url);// 设置 Curl 目标
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Curl 请求有返回的值
  8.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// 设置抓取超时时间
  9.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// 跟踪重定向
  10.     curl_setopt($ch, CURLOPT_REFERER, 'https://www.baidu.com/');//模拟来路
  11.     curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //伪造IP
  12.     curl_setopt($ch, CURLOPT_USERAGENT, $ua);// 伪造ua
  13.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// https请求 不验证证书和hosts
  14.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  15.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//强制协议为1.0
  16.     curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//强制使用IPV4协议解析域名
  17.     $content = curl_exec($ch);
  18.     curl_close($ch);// 结束 Curl
  19.     return $content;// 函数返回内容
  20. }$url = 'https://www.xiaodoubi.com/';echo curl($url);
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|不懂 ( 粤ICP备14042591号-1 )|网站地图

GMT+8, 2025-1-18 16:00 , Processed in 0.117747 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

返回顶部