PHP抓取远程网页的方法为什么要写两种呢,这都是针对不同的服务器而写的,有些服务器第一种用不了就可以用第二种,首先写的第一种抓紧方法,是兼容性比较好的。
PHP远程抓取方法一(curl_init()方法):
<?php
function open($Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, “$Date”);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1)”);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$body=open(“http://www.baidu.com/“);
echo $body;
?>
PHP远程抓取方法二(file_get_contents()方法):
<?php
$body=file_get_contents(“http://www.baidu.com/“);
echo $body;
?>
特别注意:PHP代码保存时也要与获取的网页内容编码要一致,要不然会出现乱码。

