Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\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 swoole_last_error()
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 = Swoole\Coroutine\System::dnsLookup("openswoole.com", 8);
echo $ip;
});
Common errors and mistakes when using this function:
SWOOLE_ERROR_DNSLOOKUP_RESOLVE_FAILED
: The domain name cannot be resolved, the query failed, check network connectionSWOOLE_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