调用file_get_contents(以及simple_html_dom)时返回HTTP_ACCESS [英] HTTP_ACCESS returned when invoking file_get_contents (and also simple_html_dom)

查看:81
本文介绍了调用file_get_contents(以及simple_html_dom)时返回HTTP_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我可以通过这种方式获取www.google.com的内容,不幸的是,另一个域仅向我发送此通知:

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

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

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

推荐答案

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

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

这篇关于调用file_get_contents(以及simple_html_dom)时返回HTTP_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆