76

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.

6
  • is the remote address reachable? does the DNS resolve?
    – farzad
    Commented Apr 18, 2010 at 8:10
  • 1
    Yes. In fact, I currently have it accessing a url on the same server.
    – Rob
    Commented Apr 18, 2010 at 8:12
  • 2
    I have encountered the same error but i think it was from a bad server configuration in "etc/resolf.conf" and "etc/hosts"
    – molokoloco
    Commented Mar 22, 2011 at 0:29
  • The answer is to correct your /etc/hosts. Make sure the url points to where it should (usually localhost).
    – tread
    Commented Jul 18, 2016 at 8:50
  • 1
    My mistake was very silly .... Spelling of localhost was wrong :) Commented Feb 7, 2018 at 7:49

12 Answers 12

38

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.

4
  • Well 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");
    – Rob
    Commented Apr 18, 2010 at 8:14
  • 1
    it's fine, you can just ignore the return value of file_get_contents.
    – elias
    Commented Apr 18, 2010 at 8:18
  • Thought so. I'll give it a shot and come back
    – Rob
    Commented Apr 18, 2010 at 8:18
  • 2
    @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).
    – zombat
    Commented Apr 18, 2010 at 8:20
29

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
1
  • While 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 to www.mydomain.net/1/file.php and you'll understand why). Commented Oct 22, 2012 at 2:46
24

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);
4
  • well hell that makes it all too easy. one sec I'll give it a shot
    – Rob
    Commented Apr 18, 2010 at 8:21
  • 3
    Welcome to the simple world of PHP xD
    – elias
    Commented Apr 18, 2010 at 9:05
  • 7
    This 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.
    – J-P
    Commented Jul 15, 2013 at 14:36
  • 1
    I 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. Commented Sep 15, 2014 at 23:24
12

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 
2
  • 1
    +1 you're a life saver, I upgraded php version and was wondering why getting error, restarting apache solved it. Thanks
    – Alyas
    Commented Jun 25, 2020 at 6:52
  • That's so annoying. I tried a lot of things to troubleshoot and then I saw your message. Thank you, it helped me a lot.
    – Mate Paiva
    Commented Nov 26, 2021 at 15:28
4
$url = "http://user:[email protected]/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;
3

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

2

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.

2
  • Well what I'm doing is submitting GET data to the $s['url']
    – Rob
    Commented Apr 18, 2010 at 8:07
  • it 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.
    – farzad
    Commented Apr 18, 2010 at 8:19
1

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.

1
  • 1
    I 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.
    – David
    Commented Oct 20, 2017 at 16:19
0

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

0

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

0

i was using docker and while login to db, i was getting this error. error

  • change server name from **db** to mysql.

changed server name

This helped to resolve the issue and i successfully logged in.

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Ouroborus
    Commented May 18 at 20:03
-1

I had a similar problem when connecting to a local MySQL database, only changed the url for the ip address 127.0.0.1 and it worked. You could change the url for the ip address of the server.

Not the answer you're looking for? Browse other questions tagged or ask your own question.