HTTP_ACCESS returned when invoking file_get_conten

2019-08-28 13:23发布

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条回答
Melony?
2楼-- · 2019-08-28 14:10

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"
    )
);
查看更多
登录 后发表回答