75

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Rob
  • 7,980
  • 30
  • 75
  • 115

11 Answers11

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.

zombat
  • 92,731
  • 24
  • 156
  • 164
  • 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("http://www.domain.com/file.php?action=this&get=that"); – Rob Apr 18 '10 at 08:14
  • 1
    it's fine, you can just ignore the return value of file_get_contents. – elias Apr 18 '10 at 08:18
  • Thought so. I'll give it a shot and come back – Rob Apr 18 '10 at 08: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 Apr 18 '10 at 08: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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Johan
  • 307
  • 3
  • 2
  • 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). – newfurniturey Oct 22 '12 at 02: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);
elias
  • 1,481
  • 7
  • 13
  • well hell that makes it all too easy. one sec I'll give it a shot – Rob Apr 18 '10 at 08:21
  • 3
    Welcome to the simple world of PHP xD – elias Apr 18 '10 at 09: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 Jul 15 '13 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. – Simon A. Eugster Sep 15 '14 at 23:24
10

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 
snex
  • 982
  • 11
  • 21
  • +1 you're a life saver, I upgraded php version and was wondering why getting error, restarting apache solved it. Thanks – Alyas Jun 25 '20 at 06: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 Nov 26 '21 at 15:28
4
$url = "http://user:pass@www.example.com/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;
Aurimas
  • 138
  • 7
Ap.Muthu
  • 41
  • 1
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.

farzad
  • 8,775
  • 6
  • 32
  • 41
  • Well what I'm doing is submitting GET data to the $s['url'] – Rob Apr 18 '10 at 08: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 Apr 18 '10 at 08: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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 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 Oct 20 '17 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

minhducck
  • 1
  • 1
  • 2
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

Steeve
  • 423
  • 2
  • 9
  • 23
0

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.

ajooo
  • 1
  • 1