Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine\System::dnsLookup(string $domain, float $timeout = 5): string|false
The hostname to lookup and query.
A float in seconds for how long before timing out the lookup. Because a float is used, 1.5 means 1.5 seconds. The lowest possible value is 0.001.
Returns a string of the IP address if successful and false
when an error occurs, check OpenSwoole\Util::getLastErrorCode()
for more details.
Get the IP address of a hostname.
This DNS lookup method is using the DNS set in /etc/resolve.conf
.
Only IPv4 is supported and is based on UDP network communication instead of using libc
which is used by gethostbyname
functions.
<?php
Co::set(['dns_server' => '192.0.0.1:53']);
co::run(function()
{
$ip = OpenSwoole\Coroutine\System::dnsLookup("openswoole.com", 8);
echo $ip;
});
Common errors and mistakes when using this function:
OpenSwoole\Constant::ERROR_DNSLOOKUP_RESOLVE_FAILED
: The domain name cannot be resolved, the query failed, check network connectionOpenSwoole\Constant::ERROR_DNSLOOKUP_RESOLVE_TIMEOUT
: The resolution timed out, the DNS server may be faulty, and the result cannot be returned within the specified time, increase the timeout parameter