Swoole Coroutine System

Swoole\Coroutine\System

Since version 4.4.6.
Swoole\Coroutine\System::sleep(float $seconds)

Example:

<?php
go(function() {
    Swoole\Coroutine\System::sleep(0.2);
});
Swoole\Coroutine\System::exec(string $cmd): array

The return value:

[
    'code' => 0,
    'signal' => 0,
    'output' => '',
]

Example:

<?php
go(function() {
    $ret = Swoole\Coroutine\System::exec("md5sum ".__FILE__);
    print_r($ret);
});
Swoole\Coroutine\System::gethostbyname(string $host, int $family = AF_INET, double $timeout): string | bool

Find one IP address by host name.

$family is AF_INET for IPv4 or AF_INET6 for IPv6.

Example:

<?php
go(function () {
    $ip = Swoole\Coroutine\System::gethostbyname("www.swoole.co.uk", AF_INET, 0.5);
    print_r($ip);
});
Swoole\Coroutine\System::getaddrinfo(string $host, int $family = AF_INET, int $socktype = SOCK_STREAM, int $protocol = IPPROTO_TCP, string $service = null): array | bool

Find multiple IP addresses by host name.

$family is AF_INET for IPv4 or AF_INET6 for IPv6.

Swoole\Coroutine\System::dnsLookup(string $domain, double $timeout = 5): bool

DNS lookup using the DNS in /etc/resolve.conf. Only IPv4 is supported.

The error details can be found using swoole_last_error().

Swoole\Coroutine\System::readFile(string $filename): string | bool
Swoole\Coroutine\System::writeFile(string $filename, string $fileContent, int $flags): string | bool

Maximum size of $fileContent is 4MB. $flags is FILE_APPEND if appending to the file.

Example:

<?php
use Swoole\Coroutine\System;
go(function () {
    $ret =  System::writeFile(__DIR__ ."/hello.txt", "Hello World!");
    print_r($ret);
});
Swoole\Coroutine\System::fgets(resource $handle)
Swoole\Coroutine\System::fwrite(resource $handle, string $data, int $length = 0)
Swoole\Coroutine\System::statvfs(string $path)

Example:

<?php
go(function() {
    var_dump(Swoole\Coroutine\System::statvfs('/'));
});