php的curl怎样提供header头部信息?比如采集时假的IP

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php的curl怎样提供header头部信息?比如采集时假的IP脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP的curl怎样提供header头部信息?比如采集时假的IP》要点:
本文介绍了PHP的curl怎样提供header头部信息?比如采集时假的IP,希望对您有用。如果有疑问,可以联系我们。

在使用curl采集或访问网页时,有时需要提供一些header头部信息。这需要用到curl的CURLOPT_HTTPHEADER。

下面是一个手册案例:

function disguise_curl($url) 
 { 
   $curl = curl_init(); 

   // SETUP headers - I used the same headers From Firefox version 2.0.0.6 
   // below was splIT up because PHP.net said the line was too long. :/ 
   $header[0] = "Accept: text/XMl,application/xML,application/xhtml+xml,"; 
   $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
   $header[] = "Cache-Control: ;max-age=0"; 
   $header[] = "Connection: keep-alive"; 
   $header[] = "Keep-Alive: 300"; 
   $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
   $header[] = "Accept-Language: en-us,en;q=0.5"; 
   $header[] = "PRagma: "; // browsers keep this blank. 

   curl_setopt($curl, CURLOPT_URL, $url); 
   curl_setopt($curl, CURLOPT_USERAGENT, 'GOOGLEbot/2.1 (+http://www.google.COM/bot.html)'); 
   curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
   curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); 
   curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
   curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
   curl_setopt($curl, CURLOPT_RETURNtransfer, 1); 
   curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

   $html = curl_exec($curl); // execute the curl command 
   curl_close($curl); // close the connection 

   return $html; // and finally, return $html 
 }

上面的案例中,伪造了一个假的google来

如果你需要提供一个IP,比如采集时需要伪造一个假IP。那么可以传递这两个头部信息

$header = array( 
    'CLIENT-iP:假的IP', 
    'X-FORWARDED-FOR:假的IP', 
); 

脚本宝典总结

以上是脚本宝典为你收集整理的php的curl怎样提供header头部信息?比如采集时假的IP全部内容,希望文章能够帮你解决php的curl怎样提供header头部信息?比如采集时假的IP所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。