【问题标题】:Strange file_get_contents behavior from same server来自同一服务器的奇怪 file_get_contents 行为
【发布时间】:2017-03-22 14:24:21
【问题描述】:

这个问题有点奇怪。我有一个可以在任何浏览器中打开的 URL。

我可以使用 file_get_contents() 函数从我的本地服务器或任何其他服务器获取此 URL 的内容,但是当我在同一台服务器上使用 file_get_contents() 函数运行该页面时,会出现 404 错误。

file_get_contents() 在此服务器上启用,因为它可以读取托管在其他服务器上的 URL 的内容。

让我再详细解释一下。

需要从同一服务器上的 URL2 读取我的 Web 服务器上的 URL1。

可以从任何其他服务器读取 URL1,但不能从 URL2 读取,因为这两个 URL 位于同一服务器上。

我猜这与服务器设置有关。

谢谢!

【问题讨论】:

    标签: http-status-code-404 file-get-contents


    【解决方案1】:

    你可能想试试 CURL 代替 file_get_contents() :

    function get_web_page( $url )
    {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_USERAGENT      => "spider", // who am i
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        );
    
        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );
    
        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }
    
    $x=get_web_page('http://yourserver/the_script.php');    
    echo $x["content"];
    

    【讨论】:

    • 对不起,cURL 也有同样的问题。不确定,但可能与 DNS 解析有关。
    • 也许吧。检查你的防火墙。确保它没有阻塞。
    猜你喜欢
    • 2012-02-09
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-02-10
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多