【问题标题】:file_get_contents() via proxy in httpdfile_get_contents() 通过 httpd 中的代理
【发布时间】:2012-10-10 03:44:42
【问题描述】:

我尝试使用以下代码通过代理使用 file_get_contents() 获取页面的源代码

$aContext = array(
'http' => array(
    'proxy' => 'tcp://proxy.ssn.net:8080',
    'request_fulluri' => true,
),);
$cxContext = stream_context_create($aContext);
$homepage = file_get_contents('http://www.indiaglitz.com/channels/tamil/reviews.asp', False, $cxContext);
echo $homepage;

这在我通过终端执行 php 文件时有效,但是当我使用 httpd(Apache 2)通过浏览器运行文件时,我没有得到输出。

这适用于安装在 Ubuntu 中的 Apache2,但不适用于安装在 Fedora 17 中的 httpd。

错在哪里??

【问题讨论】:

  • 你是在终端中运行还是通过浏览器在同一台服务器上运行? php 设置可以关闭非本地文件的 file_get_contents()。 (顺便说一句,您不应该使用 file_get_contents 从 url 获取文件。)

标签: php apache2 php-5.2


【解决方案1】:

最常见的原因是 SELinux 不允许 httpd 建立网络连接。

要解决此问题,您需要更改 SELinux 布尔值(该值将在重新启动后自动保留)。您可能还想重新启动 httpd 以重置代理工作者,尽管这不是严格要求的。

setsebool -P httpd_can_network_connect 1

【讨论】:

    【解决方案2】:

    所以我看到一台服务器在您的 ubuntu 上工作,但在 fedora 服务器上却没有。 fedora 服务器可能在其 php 设置中关闭了选项 Allow_url_fopen (file_get_contents 使用 fopen())。 http://php.net/manual/en/filesystem.configuration.php

    但是!不要更改该安全设置。如果你想下载一些东西,curl 可以很好地工作而不会打开安全漏洞。

    【讨论】:

    • allow_url_fopen 为 Local 和 Master 值打开。我使用 phpinfo() 进行了检查。
    • 我试过 curl,它给出了同样的错误,(即)它在终端中工作,但在浏览器中没有。
    猜你喜欢
    • 2023-03-05
    • 2015-10-16
    • 1970-01-01
    • 2018-11-25
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多