【问题标题】:file_get_contents($url) returning bool(false)file_get_contents($url) 返回 bool(false)
【发布时间】:2017-02-23 08:45:20
【问题描述】:

我在 Centos 上使用 php-5.6.30 和 Apache 2.2.15。我正在使用此代码:

 $uri = trim(urldecode($_REQUEST['uri']));
 $validate_content = @file_get_contents($uri);
 echo $validate_content;

我正在访问它:http://10.20.30.40:8001/index.html?uri="URL HERE" 每当我使用像 http://www.example.com 这样的 URL 时,它都可以正常工作。但是每当我使用像http://127.0.0.1:8000/App/app.htmlhttp://10.10.10.40:9000/App/app.html 这样的URL 时,它都会返回bool(false)。

当我在 Windows WampServer(Apache 2.4.18 - PHP 5.6.19 - MySQL 5.7.11) 上运行相同的代码(以上代码)并访问它时。它工作正常。但是当我在 Centos(Apache 2.2.15, php-5.6.17-3(cli) 中使用相同的代码时,它不起作用。

【问题讨论】:

    标签: php file-get-contents


    【解决方案1】:

    可能是 httpd 的 SELinux 设置禁止了一些出站连接。尝试检查以下设置:

    $ getsebool httpd_can_network_connect
    httpd_can_network_connect --> off
    

    如果设置为关闭,请尝试启用它:

    $ sudo setsebool -P httpd_can_network_connect on
    $ getsebool httpd_can_network_connect
    httpd_can_network_connect --> on
    

    现在您的 httpd 服务器应该可以进行传出连接了。我不确定为什么它已经可以连接到http://www.example.com,但不能连接到 localhost,但这解决了我系统上的相同问题。

    虽然这可能会解决您的问题,但请小心更改此设置和其他设置。可能存在无法预料的安全隐患,因此您应该在生产中使用此解决方案之前对其进行审查。

    您可以看到所有影响 httpd 的设置:

    $ getsebool -a | grep httpd
    

    【讨论】:

      【解决方案2】:

      使用 cURL 代替 file_get_contents。这个链接很有用PHP : How to use curl instead file_get_contents?

      【讨论】:

        【解决方案3】:
        $query = http_build_query(urldecode($_REQUEST['url']));
        

        您也可以在php.ini 文件中查看allow_url_fopen

        或者您也可以使用curl 代替file_get_contents

        【讨论】:

          猜你喜欢
          • 2013-01-25
          • 1970-01-01
          • 2016-10-24
          • 1970-01-01
          • 2019-01-09
          • 2011-12-03
          • 1970-01-01
          • 2015-12-21
          • 2012-08-24
          相关资源
          最近更新 更多