php_network_getaddresses: getaddrinfo failed: Name or service not known
Solution 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);
Solution 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.
Solution 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
Solution 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
Solution 5
$url = "http://user:[email protected]/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;
Comments
-
Rob almost 2 years
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.
-
Rob about 14 yearsWell what I'm doing is submitting GET data to the $s['url']
-
Rob about 14 yearsWell I don't need to retrieve the contents, only submit the GET data. Would that particularly matter, or would it be fine anyway? i.e. file_get_contents("domain.com/file.php?action=this&get=that");
-
elias about 14 yearsit's fine, you can just ignore the return value of file_get_contents.
-
Rob about 14 yearsThought so. I'll give it a shot and come back
-
farzad about 14 yearsit is ok to replace the hardcoded addresses in the URLs with the data you receive from GET. but make sure there would be a shchema and if not, add a default 'http://' there. use parse_url() function to make sure there is a schema in the URL.
-
zombat about 14 years@Rob - elias is right, you could just ignore the return value. I updated my answer with a crude
fsockopen()
method of manually sending HTTP request headers if you were worried about the overhead of making a full HTTP request (which probably isn't much, unless that URL is returning a ton of info). -
Rob about 14 yearswell hell that makes it all too easy. one sec I'll give it a shot
-
elias about 14 yearsWelcome to the simple world of PHP xD
-
newfurniturey over 11 yearsWhile this will work for most dns-resolution based methods, it isn't applicable with
fsockopen()
and in turn this question (to verify, make a test file that's copy+paste of the OP's code and use a web-URL similar towww.mydomain.net/1/file.php
and you'll understand why). -
J-P almost 11 yearsThis is an alternative solution to the code problem, but doesn't solve the underlying infrastructure problem (DNS lookup failing.) As I'm getting the same issue from within Drupal, hacking someone else's contributed project is an undesirable option for me: instead, I can recommend the other answer in this thread, involving /etc/hosts, which did indeed fix my DNS.
-
Simon A. Eugster over 9 yearsI just had (have) the same problem, and after running Wireshark I saw that the router did not update his DNS cache yet and the newly created subdomain wasn't resolved yet.
-
David over 6 yearsI don't think the first version of that line in /etc/nsswitch.conf should be wrong. that's how mine reads on a debian 9 installation, likewise debian 8,and networking is all good. HOWEVER, immediately following a dist upgrade from Debian 8 to 9, I was getting these errors. It turned out to be a simple matter of restarting the webserver -- in my case, Apache.
-
Alyas almost 4 years+1 you're a life saver, I upgraded php version and was wondering why getting error, restarting apache solved it. Thanks
-
Mate Paiva over 2 yearsThat's so annoying. I tried a lot of things to troubleshoot and then I saw your message. Thank you, it helped me a lot.