如何用php获取指定地址 的网页源文件?

地址 有限制没有?

第1个回答  2016-07-19
$info = file_get_contents(');

//info 就是网页的源文件可以直接输出 也可以保存

echo file_put_contents('./test.html',$info)?'保存成功':'保存失败'; //保存

第2个回答  2016-07-26
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头文件的不同而不同,其实这里有很多更加深入的设置。
官网有文档的,你可以看下更详细的说明。
第3个回答  2016-05-27
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;本回答被网友采纳
第4个回答  2016-07-05

用 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(") ;

$content  = file_get_contents(") ;

第5个回答  2016-06-03
<?php
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);
}
?>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网