Swoole Coroutine System: dnsLookup

Declaration

<?php
Swoole\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 swoole_last_error() 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 = Swoole\Coroutine\System::dnsLookup("www.swoole.co.uk", 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 connection
  • SWOOLE_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