Coroutine System: dnsLookup

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\System::dnsLookup(string $domain, float $timeout = 5): string|false

Parameters

domain

The hostname to lookup and query.

timeout

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.

Return

Returns a string of the IP address if successful and false when an error occurs, check OpenSwoole\Util::getLastErrorCode() for more details.

Description

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.


Example

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