Skip to content

Warning: fsockopen(): Unable to connect to X.X.X.X:53 (Connection refused) #23

@Legendary4226

Description

@Legendary4226

Hi !

Thank's to this project, it's really cool compared to PHP's default implementation where data isn't normalized and error handling is a black box.

I'm using the library to make a lot of requests (like gathering complete DNS zones with MX, DKIM selectors, ...) and I have the warning error that I can't catch easly like : Warning: fsockopen(): Unable to connect to 9.9.9.9:53 (Connection refused).

I instantiate the DNS object like this :

$instance = new DNSQuery($dnsServer, timeout: self::DNS_TIMEOUT, udp: !$tcp);

With TCP by default and the IP to a DNS server (like 1.1.1.1).
But sometimes, DNS servers seems to have downtimes or block my requests and the warning happens.

I'm able to handle this error by touching the PHP error handler :

try {
  set_error_handler(function ($errno, $errstr, $errfile, $errline) {
      if (str_contains(strtolower($errstr), "Unable to connect to ")) {
          throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
      }
      // Let the default PHP handler do the job
      return false;
  });
} catch (ErrorException) {} finally {
  restore_error_handler(); // Really important
}

But maybe the library could handle it by default and return a specific exception like "UnavailableDnsServerException" ?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions