利用curl函数抓取网站数据,仿造IP+伪造来源+防屏蔽

 3107

这篇教程带大家学习使用PHP的curl函数抓取网站数据,并且仿造IP、伪造来源、防屏蔽的方法,希望对小伙伴们有所帮助。


1、伪造客户端IP地址,伪造访问referer:(一般情况下这就可以访问到数据了)

  1. curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-FORWARDED-FOR:110.85.108.185', 'CLIENT-IP:110.85.108.185']);
  2. curl_setopt($curl, CURLOPT_REFERER, 'http://www.demo.com/test.php');


2、如是上面的还是不行,可能是别人抓到了真实IP,这时候我们就使用代{过}{滤}理访问。

  1. #  详细方式
  2. curl_setopt($curl, CURLOPT_PROXY, 'x.x.x.x');    //代{过}{滤}理服务器地址
  3. curl_setopt($curl, CURLOPT_PROXYPORT, 80);             //代{过}{滤}理服务器端口
  4. //curl_setopt($curl, CURLOPT_PROXYUSERPWD, ':'');      //http代{过}{滤}理认证帐号,username:password的格式
  5. curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代{过}{滤}理模式
  6. #  简写方式
  7. curl_setopt($curl, CURLOPT_PROXY, 'http://x.x.x.x:80');


3、还有一种就是用浏览器可以访问,用curl不行。(对方检查了useragent,如果没有就认为是非法来源等验证了)

  1. $useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ';
  2. $useragent.= '(KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36';
  3. curl_setopt($curl, CURLOPT_USERAGENT, $useragent);


PHP完整Curl抓取数据函数:

  1. /**
  2. * 请求接口
  3. * [url=home.php?mod=space&uid=718080]@access[/url] public
  4. * [url=home.php?mod=space&uid=952169]@Param[/url] string $url 请求地址
  5. * @param array $data 提交参数 没有get 有post
  6. * [url=home.php?mod=space&uid=155549]@Return[/url] bean|array
  7. */
  8. public function send($url='')
  9. {  
  10.     set_time_limit(0);
  11.     $curl = curl_init();
  12.     curl_setopt($curl, CURLOPT_URL, $url);
  13.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  14.     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  15.     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  16.     curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-FORWARDED-FOR:127.0.1.1', 'CLIENT-IP:127.0.1.1']);
  17.     curl_setopt($curl, CURLOPT_REFERER, 'http://www.demo.com/demo.php');
  18.     curl_setopt($curl, CURLOPT_PROXY, 'http://127.0.0.1:80');
  19.     $useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ';
  20.     $useragent.= '(KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36';
  21.     curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
  22.     if(!empty($data) && is_array($data)){
  23.         curl_setopt($curl, CURLOPT_POST, true);
  24.         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  25.     }
  26.     $html = curl_exec($curl);
  27.     if($error=curl_errno($curl)){
  28.         return false;
  29.     }
  30.     curl_close($curl);
  31.     return $html;
  32. }

以上就是利用curl函数抓取网站数据,仿造IP+伪造来源+防屏蔽的方法,大家学会了吗?


本文网址:https://www.zztuku.com/detail-9999.html
站长图库 - 利用curl函数抓取网站数据,仿造IP+伪造来源+防屏蔽
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐