Php 调用文件获取内容(以及简单html dom)时返回HTTP访问
我试图通过以下方式获取页面内容:Php 调用文件获取内容(以及简单html dom)时返回HTTP访问,php,http,file-get-contents,simple-html-dom,Php,Http,File Get Contents,Simple Html Dom,我试图通过以下方式获取页面内容: <?php include_once 'simple_html_dom.php'; $opts = array('http' => array( 'method' => 'GET', 'timeout' => 10 ) ); $domain = "http://www.espe
<?php
include_once 'simple_html_dom.php';
$opts = array('http' =>
array(
'method' => 'GET',
'timeout' => 10
)
);
$domain = "http://www.esperandoaramon.com";
//$domain = "http://www.google.com";
$context = stream_context_create($opts);
$input = @file_get_contents($domain,false,$context) or die("Could not access file: $domain");
echo($input);
?>
这个HTTP_接受快把我累死了。。。该页面在浏览器上运行良好。有什么解决办法吗?问题似乎出在另一端的站点上,而不是脚本上。我怀疑另一个站点需要一个
Accept
标题,当它没有标题时,它会失败(它与浏览器一起工作,因为浏览器总是发送该标题)。请尝试在流上下文选项中设置它:
$opts = array(
'http' => array(
'method' => 'GET',
'timeout' => 10,
'header' => "Accept: text/html\r\n"
)
);
最后,我不得不添加以下解释:“header'=>“Accept:text/html\r\n”。“用户代理:Mozilla/5.0(X11;Linux x86_64;rv:12.0)Gecko/20100101 Firefox/12.0”
$opts = array(
'http' => array(
'method' => 'GET',
'timeout' => 10,
'header' => "Accept: text/html\r\n"
)
);