【问题标题】:How can I load a remote file using file_get_contents()?如何使用 file_get_contents() 加载远程文件?
【发布时间】:2012-05-18 03:42:02
【问题描述】:

现在有点抓挠我的头。
我正在尝试从远程 url 获取文件。我可以从本地文件中保存内容,但不能从远程文件中保存。

这行得通:

file_put_contents(
    'file1.xml',
    file_get_contents('file2.xml')
);

这不是:

file_put_contents(
    'file1.xml',
    file_get_contents('http://www.domain.com/xmlapi/search.aspx?query=places&lid=38')
);

奇怪的是,我的其他脚本在第一次尝试时运行良好,但现在没有一个可以访问远程 URL。
我最近开始在 Heart Internet 上托管我的新 php 文件。我在其他服务器上也有类似的脚本,完全没有问题。

这是我得到的错误:

警告:file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sites/mydomain/myfile.php on line 5`

警告:file_get_contents(http://www.domain.com/xmlapi/search.aspx?query=places&lid=38) [function.file-get-contents]:未能打开流:php_network_getaddresses:getaddrinfo 失败:/home/sites/mydomain/public_html/myfile.php 中的名称或服务未知第 5 行`

allow_url_fopen 设置为 On

不同的域

【问题讨论】:

  • 任何错误信息?在脚本顶部添加error_reporting(E_ALL); ini_set('display_errors', 1);
  • 这两个文件是否在同一个域中?问因为我已经经历过一些网络托管服务提供商阻止了对外部域的呼叫(来自防火墙)....
  • 此类连接是否允许使用 http 包装器?
  • 您可能需要在 php.ini 中设置“allow_url_fopen = On”
  • php.ini 设置为 allow_url_fopen = On

标签: php file-get-contents file-put-contents


【解决方案1】:

警告:file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sites/mydomain/myfile.php on line 5`

这表明您使用的 URL 不正确,或者您的服务器上的 DNS 设置不正确。

【讨论】:

    【解决方案2】:

    检查 php 设置...

    可能是因为安全问题... "allow_url_fopen = On"

    http://www.php.net/manual/en/function.file-get-contents.php

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      相关资源
      最近更新 更多