Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?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
The domain name to resolve.
The domain family to use. There are two main types: AF_INET
represents IPv4 addresses and AF_INET6
represents IPv6 addresses.
The socket type, can be: SOCK_STREAM
(default), SOCK_DGRAM
or SOCK_RAW
.
The protocol that is being used. Can be STREAM_IPPROTO_TCP
(default), STREAM_IPPROTO_UDP
, STREAM_IPPROTO_STCP
or STREAM_IPPROTO_TIPC
.
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.
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.
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.
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.
<?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"
}