Coroutine System: dnsLookup

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.



Co::set(['dns_server' => '']);

    $ip = OpenSwoole\Coroutine\System::dnsLookup("", 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 connection
  • OpenSwoole\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
Last updated on September 21, 2022