file_get_contents伪造user_agent curl设置useragent的方法

file_get_contents 和 curl 这俩强悍的函数,在远程抓取时候相当有用处.不过一些网站会根据来访ip是否携带user_agent来判断是正常的浏览器客户端还是机器.所以,我们的任务就是给他们伪造user_agent.
file_get_contents伪造user_agent 方法如下:

 
  
  ini_set ( ' user_agent ' , ' Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;http://www.9qc.com) ' );
curl伪造user_agent的方法:

 
  
curl_setopt( $c , CURLOPT_USERAGENT , ' Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;http://www.9qc.com) ' );
附带上curl的完整函数方法:

代码
 
   
1 . function curl_get_file_contents( $URL )
2 . {
3 . $c = curl_init();
4 . curl_setopt( $c , CURLOPT_RETURNTRANSFER , 1 );
5 . // curl_setopt($c, CURLOPT_HEADER, 1);//输出远程服务器的header信息
6 . curl_setopt( $c , CURLOPT_USERAGENT , ' Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;http://www.9qc.com) ' );
7 . curl_setopt( $c , CURLOPT_URL , $URL );
8 . $contents = curl_exec( $c );
9 . curl_close( $c );
10 . if ( $contents ) { return $contents ;}
11 . else { return FALSE ;}
12 . }

 

这样就可以抓取了,user_agent是可以自己修改的.

转载于:https://www.cnblogs.com/xylxq1925/articles/1904539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值