Skip to content

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

Closed

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" ?

Activity

  1. self-assigned this
    on Sep 24, 2025
  2. purplepixie commented on Sep 24, 2025

    @purplepixie
    Owner

    Currently fsockopen() failing will return an error condition (error flag is true) but does not throw an exception.

    Throwing an exception may be a good idea but could be a breaking change, for example any system which relies on the error flag alone would be broken by exception becoming the default.

    Will investigate - may have a configurable setting which is off by default.

  3. Legendary4226 commented on Sep 24, 2025

    @Legendary4226
    Author

    Thank you for the quick answer :)

    I didn't event showed the first element of the stacktrace, the error is thrown at : /vendor/purplepixie/phpdns/src/PurplePixie/PhpDns/DNSQuery.php:377

  4. purplepixie commented on Sep 25, 2025

    @purplepixie
    Owner

    This has been implemented in branch #25 which has been merged back into master.

    To keep this from being a breaking change it is now optional and can be set by calling setConnectionException(true) on the DNSQuery object before performing a query.

    The usage documentation has been updated to include this information.

    This update is live in release tag 2.3.0

    @Legendary4226 thank you for raising this - does this meet your needs?

  5. Legendary4226 commented on Sep 26, 2025

    @Legendary4226
    Author

    It totally meet my need ! Thank you very much 💯

  6. purplepixie commented on Sep 28, 2025

    @purplepixie
    Owner

    Perfect, thanks for raising this enhancement request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions