php file_get_contents(): php_network_getaddresses: getaddrinfo 失败: 名称或服务未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20064372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known
提问by user3007493
I'm trying to download an image from a server using a PHP script on my website on xampp server.
我正在尝试使用 xampp 服务器上我网站上的 PHP 脚本从服务器下载图像。
The image is being downloaded using the function file_get_contents.
正在使用函数 file_get_contents 下载图像。
The php code for downloading on the server is:
服务器上下载的php代码为:
if(isset($_GET['path']) && isset($_GET['username'])) {
echo "path:".$_GET['path'];
$temp = explode(".", $_GET['path']);
$extension = end($temp);
$fname="images/".$_GET['title'];
$filenameIn = $_GET['path'];
$filenameOut = "" . $fname;
$contentOrFalseOnFailure = file_get_contents($filenameIn);
$byteCountOrFalseOnFailure = file_put_contents($filenameOut,$contentOrFalseOnFailure);
}
But I'm getting this error:
但我收到此错误:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/xampp/project/upload_art.php on line 19
Warning: file_get_contents(http://app6.pixlr.com/_temp/528afa6e2f7cc6a5b1000101.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/xampp/project/upload_art.php on line 19
警告:file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/xampp/project/upload_art.php on line 19
警告:file_get_contents(http://app6.pixlr.com/_temp/528afa6e2f7cc6a5b1000101.jpg):无法打开流:php_network_getaddresses:getaddrinfo失败:名称或服务在/opt/lampp/htdocs/xampart/project/upload_中未知第 19 行的 php
I have checked out all posted answers here but none seems to resolve the issue. Please help!
我已经在这里查看了所有已发布的答案,但似乎没有一个能解决问题。请帮忙!
回答by Siraj Khan
It means your server cannot connect to the outside world
这意味着您的服务器无法连接到外界
This probably won't change anything given the DNS issues
鉴于 DNS 问题,这可能不会改变任何事情
So, If you have permission, try changing the name servers in your /etc/resolv.conf file to other nameservers.
因此,如果您有权限,请尝试将 /etc/resolv.conf 文件中的名称服务器更改为其他名称服务器。
回答by Stepan Hruska
There should be as well httpd allowed to connect outside. Check you selinux policy. this helps me to solve connection problem:
也应该允许 httpd 连接到外部。检查您的 selinux 政策。这有助于我解决连接问题:
setsebool -P nis_enabled 1
setsebool -P httpd_can_network_connect 1
回答by Rasclatt
In reference to one of the comments by @DUzun, and for those like myself who are familiar with the server in general but not a computer science major, I fixed the issue on Linux/Centos6 (Plesk Onyx)by:
参考@DUzun 的评论之一,对于像我这样熟悉服务器但不是计算机科学专业的人,我通过以下方式修复了Linux/Centos6 (Plesk Onyx) 上的问题:
1) Opening /etc/resolv.conf(my file was empty)
1)打开/etc/resolv.conf(我的文件是空的)
2) Added:
2) 新增:
nameserver 8.8.8.8
nameserver 8.8.4.4
options rotate
options timeout:3
Hopefully this helps someone out there.
希望这可以帮助那里的人。
回答by Lauris Kuznecovs
Here are advice in case you are using Dockerenv for your web app. I had same problem when I used docker php image, added dns options in yml file solved my problem.
如果您在 Web 应用程序中使用Dockerenv,这里有一些建议。我在使用 docker php 图像时遇到了同样的问题,在 yml 文件中添加了 dns 选项解决了我的问题。
docker-compose.yml
docker-compose.yml
version: '2'
services:
webserver:
build: ./docker/webserver
image: my_webapp
dns: 8.8.8.8
dns_opt:
- use-vc
- no-tld-query
ports:
- 80:80
- 443:443
volumes:
- ./:/var/www/html
回答by Imdad malik
If you are using a name instead of an IP address in your smtp_host fsockopen() call, and your server cannot resolve that name into an IP address. If possible, find out the IP of the server you are trying to connect to, and use that instead. Hope so it will work fine. Example
如果您在 smtp_host fsockopen() 调用中使用名称而不是 IP 地址,并且您的服务器无法将该名称解析为 IP 地址。如果可能,找出您尝试连接的服务器的 IP,然后使用它。希望它会正常工作。 例子
$config = Array(
'protocol' => 'smtp',
'smtp_host' => '166.62.73.4', // mail.servername.com
'smtp_port' => 587,
'smtp_timeout' => '7',
'smtp_user' =>'[email protected]',
'smtp_pass' => 'password',
'_smtp_auth' => false,
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'crlf' => '\r\n',
'newline' => "\r\n"
);

