You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warning: fsockopen(): Unable to connect to X.X.X.X:53 (Connection refused) #23
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).
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 ")) {
thrownew \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
// Let the default PHP handler do the jobreturnfalse;
});
} catch (ErrorException) {} finally {
restore_error_handler(); // Really important
}
But maybe the library could handle it by default and return a specific exception like "UnavailableDnsServerException" ?
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.
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.
Activity
purplepixie commentedon Sep 24, 2025
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.
Legendary4226 commentedon Sep 24, 2025
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:377purplepixie commentedon Sep 25, 2025
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?
Legendary4226 commentedon Sep 26, 2025
It totally meet my need ! Thank you very much 💯
purplepixie commentedon Sep 28, 2025
Perfect, thanks for raising this enhancement request.