悠悠楠杉
网站页面
当使用php5自带的file_get_contents
方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP request failed!
这个警告信息。
file_get_contents
和 curl
这俩强悍的函数,在远程抓取时候相当有用处.不过一些网站会根据来访ip是否携带user_agent
来判断是正常的浏览器客户端还是机器.所以,我们的任务就是给他们伪造user_agent
。
file_get_contents
伪造user_agent
方法如下:
ini_set('user_agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');
curl
伪造user_agent
的方法:
curl_setopt($c, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');