php_network_getaddresses: getaddrinfo failed: Name or service not known

故事扮演 提交于 2019-12-17 10:34:17

问题


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

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

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


回答1:


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

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



回答2:


You cannot open a connection directly to a path on a remote host using fsockopen. The url www.mydomain.net/1/file.php contains a path, when the only valid value for that first parameter is the host, 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.

If you only want to send an HTTP request and ignore the response, you could use fsockopen() and manually send the HTTP request headers, ignoring any response. It might be easier with cURL though, 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:

$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.




回答3:


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

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

In the file /etc/hosts I added:

192.168.1.1 www.testdomain.loc

Hope this helps anyone




回答4:


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

for Centos 6 and 7:

service php-fpm restart 



回答5:


$url = "http://user:pass@www.example.com/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;



回答6:


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:

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.

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.

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.

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.




回答7:


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

  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

hope it will solve your problem

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




回答8:


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

I've been replaced string

hosts:          files myhostname mdns4_minimal [NOTFOUND=return] dns

with

hosts:          files dns

and everything works right now.




回答9:


Try to set ENV PATH. Add PHP path in to 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

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




回答10:


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



来源:https://stackoverflow.com/questions/2661546/php-network-getaddresses-getaddrinfo-failed-name-or-service-not-known

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!