Swoole Coroutine System: getaddrinfo

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\System::getaddrinfo(string $domain, int $family = AF_INET, int $sockType = SOCK_STREAM, int $protocol = STREAM_IPPROTO_TCP, string $service = null, float $timeout = -1): array|false

Parameters

domain

The domain name to resolve.

family

The domain family to use. There are two main types: AF_INET represents IPv4 addresses and AF_INET6 represents IPv6 addresses.

sockType

The socket type, can be: SOCK_STREAM (default), SOCK_DGRAM or SOCK_RAW.

protocol

The protocol that is being used. Can be STREAM_IPPROTO_TCP (default), STREAM_IPPROTO_UDP, STREAM_IPPROTO_STCP or STREAM_IPPROTO_TIPC.

service

By default this is null which means no port numbers are returned. Referrer to the manpage for getaddrinfo to learn how to use this parameter.

timeout

A float in seconds for how long before timing out the lookup. Because a float is used, 1.5 means 1.5 seconds. By default -1 means don't time out. The lowest possible value is 0.001.

Return

When successful it returns multiple IP addresses as an array. If there was an error then false is returned. Use swoole_last_error() to see what went wrong.

Description

Get the IP addresses of a hostname.

The difference with this function is that it can return multiple IP addresses, for more information on its usage and different parameters, see the getaddrinfo manpage. There are more options compared to gethostbyname.

You can also use Swoole\Coroutine\System::dnsLookup to get IP of a hostname.


Example

<?php

Co\run(function()
{
    $ips = Swoole\Coroutine\System::getaddrinfo("openswoole.com");
    var_dump($ips);
});

Output:

<?php

array(2) {
  [0]=>
  string(12) "104.21.94.90"
  [1]=>
  string(14) "172.67.221.150"
}
Last updated on August 31, 2022