HTTP_ACCESS returned when invoking file_get_contents (and also simple_html_dom)

帅比萌擦擦* 提交于 2019-12-12 02:54:50

问题


I'm trying to get the contents of a page this way:

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

I can get www.google.com contents this way, unfortunately the other domain gives me only this notification:

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

This HTTP_ACCEPT is killing me... the page runs perfectly on a browser. Is there any workaround?


回答1:


It seems that the problem is with the site at the other end, not with your script. I suspect that the other site expects an Accept header and when it doesn't have one, it fails (it works with your browser because the browser always sends that header.) Try setting it in your stream context options:

$opts = array(
    'http' => array(
        'method' => 'GET',
        'timeout' => 10,
        'header' => "Accept: text/html\r\n"
    )
);


来源:https://stackoverflow.com/questions/16242048/http-access-returned-when-invoking-file-get-contents-and-also-simple-html-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!