【问题标题】:PHP file get contents with URL returns getaddrinfo failurePHP 文件通过 URL 获取内容返回 getaddrinfo 失败
【发布时间】:2011-04-17 09:22:27
【问题描述】:

我正在尝试获取外部 PHP 文件的(预处理)内容:

file_get_contents('http://www.example.org/myfile.php');

当我这样做时,我得到一个错误:

警告:file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: nodename or servname provided, or not known in /Applications/XAMPP/xamppfiles/htdocs/...localfile.php on line 13

还有:

警告:file_get_contents(http://www.example.org/myfile.php) [function.file-get-contents]:未能打开流:php_network_getaddresses:getaddrinfo 失败:提供节点名或服务名,或在/Applications/XAMPP/xamppfiles/htdocs/.../localfile.php 第 13 行

有什么想法可以做吗?

提前致谢!

编辑:我确实将 allow_url_fopen 设置为“开启”。

【问题讨论】:

  • 你的 php.ini 中有 allow_url_fopen = On 吗?
  • 这很可能是 DNS 问题。
  • 您可能处于禁用 allow_url_fopen 的安全模式,请改用 cURL。

标签: php http file-get-contents


【解决方案1】:

你或许可以使用 cURL...

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $return = curl_exec($ch);
    curl_close ($ch);
    return $return;
}


$string = curl('http://www.example.org/myfile.php'); //string with data

【讨论】:

  • 谢谢,我会尽快尝试它是否有效:D 顺便说一句:curl_close($ch) 是一个无法访问的声明 ^^
  • 不,不是:) 只有 exit() 会终止函数。
猜你喜欢
  • 2014-05-14
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 2015-08-26
相关资源
最近更新 更多