1、 file_get_contents获取
- $url="http://www.baidu.com/";
- $fh= file_get_contents('http://www.hxfzzx.com/news/fzfj/');
- echo $fh;
2、使用fopen获取网页源代码
- $url="http://www.baidu.com/";
- $handle = fopen ($url, "rb");
- $contents = "";
- while (!feof($handle)) {
- $contents .= fread($handle, 8192);
- }
- fclose($handle);
- echo $contents;
3、使用CURL获取网页源代码
- $url="http://www.baidu.com/";
- $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
-
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_ENCODING, '');
-
-
- curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
-
-
- $data = curl_exec($curl);
- echo $data;
-
- curl_close($curl);
转载自:
引用:
PHP抓取采集类snoopy介绍: http://www.nowamagic.net/librarys/veda/detail/855
PHP获取网页内容的几种方法:
http://www.oschina.net/code/snippet_861770_19638
http://www.webkaka.com/tutorial/php/2013/052534/
UserAgent设置不当导致php抓取网页失败:
http://www.webkaka.com/tutorial/php/2013/111846/