php php_network_getaddresses:getaddrinfo 失败:名称或服务未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2661546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:14:55  来源:igfitidea点击:

php_network_getaddresses: getaddrinfo failed: Name or service not known

phpfsockopen

提问by Rob

Here is a snippet of my code

这是我的代码片段

$fp = fsockopen($s['url'], 80, $errno, $errstr, 5);
if($fp){
        fwrite($fp, $out);
        fclose($fp);

When I run it, it outputs:

当我运行它时,它输出:

unable to connect to www.mydomain.net/1/file.php:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known

无法连接到 www.mydomain.net/1/file.php:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known

I'm using this to submit GET data to the $s['url']

我用它来提交 GET 数据到 $s['url']

I can't figure out why. Any help would be greatly appreciated.

我不明白为什么。任何帮助将不胜感激。

采纳答案by elias

If you only want to submit GET data to the URL, you should use something straightforward like file_get_contents();

如果你只想向 URL 提交 GET 数据,你应该使用像 file_get_contents(); 这样简单的东西。

$myGetData = "?var1=val1&var2=val2";
file_get_contents($url.$myGetData);

回答by zombat

You cannot open a connection directly to a pathon a remote host using fsockopen. The url www.mydomain.net/1/file.phpcontains a path, when the only valid value for that first parameter is the host, www.mydomain.net.

您不能使用 直接打开远程主机上路径的连接fsockopen。urlwww.mydomain.net/1/file.php包含一个路径,当第一个参数的唯一有效值是主机时www.mydomain.net

If you are trying to access a remote URL, then file_get_contents()is your best bet. You can provide a full URL to that function, and it will fetch the content at that location using a normal HTTP request.

如果您尝试访问远程 URL,那么file_get_contents()是您最好的选择。您可以提供该函数的完整 URL,它将使用普通 HTTP 请求在该位置获取内容。

If you only want to send an HTTP request and ignore the response, you coulduse fsockopen()and manually send the HTTP request headers, ignoring any response. It might be easier with cURLthough, or just plain old fopen(), which will open the connection but not necessarily read any response. If you wanted to do it with fsockopen(), it might look something like this:

如果您只想发送 HTTP 请求并忽略响应,您可以使用fsockopen()并手动发送 HTTP 请求标头,忽略任何响应。不过,使用cURL可能更容易,或者只是简单的旧fopen(),这将打开连接但不一定读取任何响应。如果你想用 来做fsockopen(),它可能看起来像这样:

$fp = fsockopen("www.mydomain.net", 80, $errno, $errstr, 30);
fputs($fp, "GET /1/file.php HTTP/1.1\n");
fputs($fp, "Host: www.mydomain.net\n");
fputs($fp, "Connection: close\n\n"); 

That leaves any error handling up to you of course, but it would mean that you wouldn't waste time reading the response.

当然,任何错误处理都取决于您,但这意味着您不会浪费时间阅读响应。

回答by Johan

I had a similar problem on my local testserver and local testdomains e.g.: www.testdomain.loc with the function GetImageSize();

我在本地 testserver 和本地 testdomains 上遇到了类似的问题,例如:www.testdomain.loc 带有函数GetImageSize()

Solved it by adding the hostname in the hosts file on the local server:

通过在本地服务器上的主机文件中添加主机名来解决它:

In the file /etc/hostsI added:

在我添加的文件/etc/hosts 中

192.168.1.1 www.testdomain.loc

Hope this helps anyone

希望这可以帮助任何人

回答by Sanya Snex

Had such a problem (with https://github.com/PHPMailer/PHPMailer), just reload PHP and everything start worked

有这样的问题(使用https://github.com/PHPMailer/PHPMailer),只需重新加载 PHP 即可开始工作

for Centos 6 and 7:

对于 Centos 6 和 7:

service php-fpm restart 

回答by Ap.Muthu

$url = "http://user:[email protected]/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;

回答by farzad

you are trying to open a socket to a file on the remote host which is not correct. you could make a socket connection (TCP/UDP) to a port number on a remote host. so your code should be like this:

您正在尝试打开一个指向远程主机上不正确文件的套接字。您可以与远程主机上的端口号建立套接字连接 (TCP/UDP)。所以你的代码应该是这样的:

fsockopen('www.mysite.com', 80);

if you are trying to create a file pointer resource to a remote file, you may use the fopen() function. but to do this, you need to specify the application protocol as well.

如果您尝试创建指向远程文件的文件指针资源,您可以使用 fopen() 函数。但要做到这一点,您还需要指定应用程序协议。

PHP provides default stream wrappers for URL file opens. based on the schema of the URL the appropriate stream wrapper will be called internally. the URL you are trying to open does not have a valid schema for this solution. make sure there is a schema like "http://" or "ftp://" in it.

PHP 为打开的 URL 文件提供了默认的流包装器。根据 URL 的模式,将在内部调用适当的流包装器。您尝试打开的 URL 没有此解决方案的有效架构。确保其中有像“http://”或“ftp://”这样的模式。

so the code would be like this:

所以代码会是这样的:

$fp = fopen('http://www.mysite.com/path/file.txt');

Besides I don't think the HTTP stream wrapper (that handles actions on file resources on URLs with http schema) supports writing of data. you can use fread() to read contents of a the URL through HTTP, but I'm not sure about writing.

此外,我认为 HTTP 流包装器(使用 http 架构处理 URL 上的文件资源的操作)不支持写入数据。您可以使用 fread() 通过 HTTP 读取 URL 的内容,但我不确定如何编写。

EDIT: from comments and other answers I figured out you would want to send a HTTP request to the specified URL. the methods described in this answer are for when you want to receive data from the remote URL. if you want to send data, you can use http_request()to do this.

编辑:从评论和其他答案中我发现您想要向指定的 URL 发送 HTTP 请求。此答案中描述的方法适用于您希望从远程 URL 接收数据的情况。如果你想发送数据,你可以使用http_request()来做到这一点。

回答by Ali Khurram

I was getting the same error of fsocket() and I just updated my hosts files

我遇到了与 fsocket() 相同的错误,我刚刚更新了我的主机文件

  1. I logged via SSH in CentOS server. USERNAME and PASSWORD type
  2. cd /etc/
  3. ls                                     //"just to watch list"
  4. vi hosts                                   //"edit the host file"
  5. i                                    //" to put the file into insert mode"
  6. 95.183.24.10                 [mail_server_name] in my case ("mail.kingologic.com")
  7. Press ESC Key
  8. press ZZ
  1. 我在 CentOS 服务器上通过 SSH 登录。用户名和密码类型
  2. cd /etc/
  3. ls //"只是为了观看列表"
  4. vi hosts //“编辑主机文件”
  5. i //" 将文件置于插入模式"
  6. 95.183.24.10 [mail_server_name] 在我的情况下(“mail.kingologic.com”)
  7. 按ESC键
  8. 按ZZ

hope it will solve your problem

希望它能解决你的问题

for any further query please ping me at http://kingologic.com

如有任何进一步查询,请在http://kingologic.com 上ping 我

回答by Constantin Boulatnikoff

In my case this error caused by wrong /etc/nsswitch.conf configuration on debian.

在我的情况下,这个错误是由 debian 上的错误 /etc/nsswitch.conf 配置引起的。

I've been replaced string

我已被替换字符串

hosts:          files myhostname mdns4_minimal [NOTFOUND=return] dns

with

hosts:          files dns

and everything works right now.

现在一切正常。

回答by Steeve

in simple word your site has been blocked to access network. may be you have automated some script and it caused your whole website to be blocked. the better way to resolve this is contact that site and tell your issue. if issue is genuine they may consider unblocking

简而言之,您的站点已被阻止访问网络。可能是你已经自动化了一些脚本,它导致你的整个网站被阻止。解决此问题的更好方法是联系该站点并告诉您的问题。如果问题是真实的,他们可能会考虑解除封锁

回答by DucTM

Try to set ENV PATH. Add PHP path in to ENV PATH.

尝试设置ENV PATH。将 PHP 路径添加到 ENV PATH 中。

In order for this extension to work, there are DLL files that must be available to the Windows system PATH. For information on how to do this, see the FAQ entitled "How do I add my PHP directory to the PATH on Windows". Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the system's PATH), this is not recommended. This extension requires the following files to be in the PATH: libeay32.dll

为了使此扩展能够工作,必须有一些 DLL 文件可用于 Windows 系统 PATH。有关如何执行此操作的信息,请参阅标题为“如何将我的 PHP 目录添加到 Windows 上的 PATH”的常见问题解答。虽然将 DLL 文件从 PHP 文件夹复制到 Windows 系统目录中也有效(因为系统目录默认在系统的 PATH 中),但不建议这样做。此扩展需要以下文件在 PATH 中:libeay32.dll

http://php.net/manual/en/openssl.installation.php

http://php.net/manual/en/openssl.installation.php