1. Go to this page and download the library: Download phasync/server library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phasync\Net\listen(string $address, array $options = []): Listener
$listener->accept(?string &$peer = null) // connection; $peer is set to its address; IOException once closed
foreach ($listener as $peer => $conn) // until the listener is closed
$listener->close(): void
$listener->addr(): string
Listener::fromStream($stream): Listener // wrap a listening socket another process created
use function phasync\Net\serve;
phasync::run(function () {
foreach (serve('0.0.0.0:8080') as $peer => $conn) {
phasync::go(function () use ($conn) {
while ('' !== ($data = $conn->read())) {
$conn->write($data); // echo
}
$conn->close();
});
}
});
use function phasync\Net\dial;
phasync::run(function () {
$conn = dial('example.com:80');
$conn->write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");
while ('' !== ($data = $conn->read())) {
echo $data;
}
$conn->close();
});
dial('tls://example.com:443'); // TLS; the certificate is checked against the host name
dial('unix:///run/app.sock'); // Unix domain socket
dial('[::1]:8080', timeout: 5); // the timeout covers lookup, connect and TLS handshake
// context escapes to raw stream context options, such as a TLS trust store
dial('tls://example.com:443', options: ['context' => ['ssl' => ['cafile' => '/path/ca.pem']]]);
use function phasync\Net\listenPacket;
phasync::run(function () {
$conn = listenPacket('0.0.0.0:9000');
foreach ($conn as $peer => $data) {
$conn->writeTo("Echo: $data", $peer);
}
});
phasync\Net\listenPacket(string $address, array $options = []): PacketConn
$conn->readFrom(int $maxLength = 65536): array{string, string} // [datagram, peer]; IOException once closed
foreach ($conn as $peer => $data) // until the socket is closed
$conn->writeTo(string $data, string $address): int|false
$conn->close(): void
$conn->addr(): string
use phasync\Net\Dns;
use phasync\Net\DnsRecordType;
phasync::run(function () {
// Default: tries A (IPv4) first, then AAAA (IPv6).
// Returns a random IP when there are several records.
$ip = Dns::resolve('example.com');
$allIps = Dns::resolveAll('example.com', DnsRecordType::A);
$ipv4 = Dns::resolve('example.com', DnsRecordType::A);
$ipv6 = Dns::resolve('example.com', DnsRecordType::AAAA);
});