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的完整函数方法:
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($c, CURLOPT_HEADER, 1);//输出远程服务器的header信息
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_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);
if ($contents) {return $contents;}
else {return FALSE;}
}
这样就可以抓取了,user_agent是可以自己修改的.
分享到:
相关推荐
方法:编辑php.ini,设置allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。如果你使用的是虚拟主机可以考虑用curl函数来代替。curl函数的使用示例:复制代码 代码如下:$ch =...
下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:< ?php$file_contents = file_get_contents(‘https://www.jb51.net’);echo $file_...
*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_setopt( $curlHandle , CURLOPT_URL, $url ); curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $curl...
下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:< ?php$file_contents = file_get_contents(‘https://www.jb51.net’);echo $file_...
错误: Warning: fopen() [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it ...3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,
本文实例讲述了php中file_get_contents与curl性能比较分析。分享给大家供大家参考。具体如下: 在php中如果不仔细的去分析性能会发现file_get_contents与curl两个同很多共同点的,他们都可以采集文件打开文件,但是...
另外,curl的性能比file_get_contents高,所以用curl重写file_get_contents function _file_get_contents($s) { $ret = ; $ch = curl_init($s); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch...
本文实例讲述了php采用file_get_contents代替使用curl的方法,分享给大家供大家参考。具体实现方法如下: file_get_contents代替使用curl其实不多见了,但有时你碰到服务器不支持curl时我们可以使用file_get_...
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求也不大,所以没有学习使用curl。...
$ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页里需要增加...
curl多用于互联网网页之间的抓取,fopen多用于读取文件,而file_get_contents多用于获取静态页面的内容。 1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息...
今天小编就为大家分享一篇解决PHP curl或file_get_contents下载图片损坏或无法打开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码1、解决方法,找个ungzip的函数来转换下2、给你的url加个前缀,这样调用$content = file_get_contents(“compress....
今天因工作需要,需要用 curl / file_get_contents 获取需要授权(Authorization)的页面内容,解决后写了这篇文章分享给大家。 PHP curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。 ...
string file_get_contents(string $ filename,bool $ include_path = false,resource $ context,int $ offset = 0,int $ maxlen) filename是文件或URL的名称。 include_path如果启用,则在include_path中...
主要介绍了PHP中使用file_get_contents抓取网页中文乱码问题解决方法,可以通过使用curl配置gzip选项来解决,具有一定的参考借鉴价值,需要的朋友可以参考下
下面我们以.txt文件为实例来介绍php读取本地文件的函数,读取文件我们可以利用fopen或file_get_contents来读取,file_get_contents更简单而fopen需要fread配合才可以显示读出的内容。1.首先来介绍一下fopen()...