0

我正在尝试以这种方式获取页面的内容:

<?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);
?>

我可以通过这种方式获取 www.google.com 的内容,不幸的是,其他域只给了我这个通知:

Notice:
Text: Undefined index: HTTP_ACCEPT
File: /home/trdeport/public_html/esperandoaramon/_visit.php
Line: 4

这个 HTTP_ACCEPT 害死我了……页面在浏览器上完美运行。有什么解决方法吗?

4

1 回答 1

1

似乎问题在于另一端的站点,而不是您的脚本。我怀疑另一个站点需要一个Accept标头,当它没有标头时,它会失败(它适用于您的浏览器,因为浏览器总是发送该标头。)尝试在您的流上下文选项中设置它:

$opts = array(
    'http' => array(
        'method' => 'GET',
        'timeout' => 10,
        'header' => "Accept: text/html\r\n"
    )
);
于 2013-04-26T18:01:10.280 回答