Coroutine System: getaddrinfo

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

Declaration

<?php OpenSwoole\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 OpenSwoole\Util::getLastErrorCode() 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 OpenSwoole\Coroutine\System::dnsLookup to get IP of a hostname.


Example

<?php

co::run(function()
{
    $ips = OpenSwoole\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 September 21, 2022