$info = file_get_contents('https://zhidao.baidu.com/question/1513631908043601820.html?entry=&ishq=1');
//info 就是网页的源文件可以直接输出 也可以保存
echo file_put_contents('./test.html',$info)?'保存成功':'保存失败'; //保存
curl函数库
根据你要的网页产生的Http头信息,设置curl的参数,举一个简单例子
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
print_r($output);
curl的设置选项根据你要请求的Http头文件的不同而不同,其实这里有很多更加深入的设置。
官网有文档的,你可以看下更详细的说明。
php已提供相关函数。
file_get_contents() 函数把整个文件读入一个字符串中。
$ret = file_get_contents('要采集的网页URL');
// 若需要从页面中获取内容,可以用正则匹配
$begin=change_match_string('匹配开头的字符串');
$end=change_match_string('匹配结尾的字符串');
$p = "{$begin}(.*){$end}";
// 使用正则进行匹配
if (eregi($p,$ret,$rs)) return $rs[1];
else return false;
用 file_get_contents()
也可以用 curl 发送http请求获取内容
如
function http_post($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$rtn = curl_exec($ch);
if ($errno = curl_errno($ch)) {
throw new Exception(curl_error($ch), $errno);
}
curl_close($ch);
return $rtn;
}
$content = http_post("http://www.baidu.com") ;
$content = file_get_contents("http://www.baidu.com") ;
function pipei($url){
$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_USERAGENT,"Mozilla/5.0?(Linux;?U;?Android?4.1.1;?zh-cn;?MI?2S?Build/JRO03L)?AppleWebKit/534.30?(KHTML,like?Gecko)?Version/4.0?Mobile?Safari/534.30?XiaoMi/MiuiBrowser/1.0");
$str=curl_exec($curl);
curl_close($curl);
}
?>