代码之家  ›  专栏  ›  技术社区  ›  arik

php ini文件获取外部URL

  •  36
  • arik  · 技术社区  · 14 年前

    我使用以下PHP函数:

    file_get_contents('http://example.com');

    每当我在某个服务器上执行此操作时,结果都是空的。当我在其他地方做的时候,结果是不管页面的内容是什么。但是,当我在结果为空的服务器上,在本地使用该函数-而不访问外部URL( file_get_contents('../simple/internal/path.html'); ) 工作。

    现在,我非常确定它与某个php.ini配置有关。但我不确定的是, 哪一个 一个。请帮忙。

    7 回复  |  直到 5 年前
        1
  •  39
  •   Aillyn    14 年前

    你要找的环境是 allow_url_fopen .

    有两种方法可以在不更改php.ini的情况下绕过它,其中一种方法是使用 fsockopen() 另一个是使用 cURL .

    我建议用卷发 file_get_contents() 无论如何,因为它是为这个而建造的。

        2
  •  30
  •   maxisme    9 年前

    作为对Ailyn答案的补充,您可以使用类似下面的函数来模拟文件获取内容的行为:

    function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
    }
    
    echo get_content('http://example.com');
    
        3
  •  4
  •   Artefacto    14 年前

    与ini配置设置相关 allow_url_fopen .

    您应该知道,启用该选项可能会使代码中的一些错误成为可利用的。

    例如,验证输入失败可能会变成一个成熟的远程代码执行漏洞:

    copy($_GET["file"], "."); 
    
        4
  •  4
  •   Alireza Fallah    10 年前
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    

    最适合HTTP URL, 但是如何打开https url帮助我

        5
  •  2
  •   J-a-n-u-s    7 年前

    上面提供的答案解决了问题,但不能解释操作描述的奇怪行为。这个解释可以帮助任何人在开发环境中测试站点之间的通信,在开发环境中,这些站点都驻留在同一台主机上(和同一个虚拟主机;我正在使用Apache2.4和php7.0)。

    有一种微妙的 file_get_contents() 我在这里发现这是绝对相关的,但没有解决(可能是因为它几乎没有文档记录,或者没有文档记录,或者是在一个模糊的PHP安全模型白皮书中,我找不到)。

    allow_url_fopen 设置为 Off 在所有相关环境中(例如 /etc/php/7.0/apache2/php.ini , /etc/php/7.0/fpm/php.ini 等)和 允许访问权限 设置为 On 在命令行上下文中(即 /etc/php/7.0/cli/php.ini ) 文件获取目录() 对于本地资源,不允许记录任何警告,例如:

    file_get_contents('php://input');
    

    // Path outside document root that webserver user agent has permission to read. e.g. for an apache2 webserver this user agent might be www-data so a file at /etc/php/7.0/filetoaccess would be successfully read if www-data had permission to read this file
    file_get_contents('<file path to file on local machine user agent can access>');
    

    // Relative path in same document root
    file_get_contents('data/filename.dat')
    

    总之,限制 allow_url_fopen = Off 类似于 iptables 规则在 OUTPUT 链,其中限制仅在尝试“退出系统”或“更改上下文”时应用。

    N.B. 允许访问权限 设置为 在命令行上下文中(即 /etc/php/7.0/cli/php.ini文件 )是我的系统所拥有的,但我怀疑这与我提供的解释没有关系,即使它被设置为 下车 当然,除非通过从命令行本身运行脚本进行测试。我没有用 允许访问权限 设置为 下车 在命令行上下文中。

        6
  •  1
  •   Wayne Nort    12 年前

    这也将给外部链接一个绝对路径,而不必使用php.ini。

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.your_external_website.com/$2$3', $result);
    echo $result
    ?>
    
        7
  •  0
  •   Kristian Glass adrianz    12 年前

    添加:

    allow_url_fopen=1
    

    在你 php.ini 文件。如果您正在使用共享宿主,请先创建一个。