【问题标题】:Problems with PHP file_get_contents() on RHEL 6RHEL 6 上的 PHP file_get_contents() 问题
【发布时间】:2012-05-01 13:28:07
【问题描述】:

好的,我有两个 Linux 机器在代理服务器后面运行。两个框都设置为通过连接端口 801 绕过过滤。

方框 A - Fedora Core 12 / PHP 5.3.1

方框 B - RHEL 6 / PHP 5.3.3

在 Box A 上,我可以使用 file_get_contents() 连接到外部站点。

<?php
$opts = array(
              'http' => array(
                              'proxy' => 'tcp://10.136.132.1:801',
                              'request_fulluri' => true
                             )
             );

$cxContext = stream_context_set_default($opts);
echo file_get_contents("http://www.google.com");

这会显示 Google 的主页。

在 Box B 上,我运行相同的代码,但出现此错误:

Warning: file_get_contents(http://www.google.com): failed to open stream: Permission denied

两个盒子都在同一个网络上,在同一个代理服务器后面。我在 Apache 或 PHP 中是否缺少允许 file_get_contents 在 Box B 上工作的设置?

【问题讨论】:

  • print_r($http_response_header) 告诉你什么?
  • 在框 B $http_response_header 是一个未定义的变量。在 Box A 上,它在数组中生成标题信息。是否需要打开某些东西才能使其正常工作?
  • 如果响应头指示 4xx 错误,则可能不是 PHP 问题
  • 我什么也没得到——在 Box B 上,PHP 没有将 $http_response_header 视为系统函数。它认为它是一个没有被声明的变量

标签: php linux apache proxy file-get-contents


【解决方案1】:

听起来您启用了 SELinux,默认情况下它会阻止 Apache 的任何传出连接。尝试在你的 shell 中以 root 身份运行它:

setsebool -P httpd_can_network_connect on

可以在此处找到有关 SELinux 布尔值的更多信息: http://wiki.centos.org/TipsAndTricks/SelinuxBooleans

【讨论】:

  • 那行得通。 SELinux 确实是问题所在。感谢您对 Kaivosukeltaja 的帮助。非常感谢!
【解决方案2】:

这可能是因为 google 阻止了似乎来自 Bot 或 Script 的请求。 可能是因为 Box A 在向 google 请求时设置了额外的标头。

尝试在 Box b 上打开其他网站

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-13
    • 2011-07-09
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多