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/ */
use phasync\Net\Dns;
use phasync\Net\DnsRecordType;
phasync::run(function() {
// Default: tries A (IPv4) first, then AAAA (IPv6)
// Returns random IP when multiple records exist (load balancing)
$ip = Dns::resolve('example.com');
// Get all IPs for a hostname
$allIps = Dns::resolveAll('example.com', DnsRecordType::A);
// Explicit IPv4 only
$ipv4 = Dns::resolve('example.com', DnsRecordType::A);
// Explicit IPv6 only
$ipv6 = Dns::resolve('example.com', DnsRecordType::AAAA);
});
use phasync\Net\UdpServer;
phasync::run(function() {
$server = new UdpServer('0.0.0.0:9000');
foreach ($server->receive() as $peer => [$data, $socket]) {
// Reply using the same socket (correct interface routing)
stream_socket_sendto($socket, "Echo: $data", 0, $peer);
}
});
$server = new UdpServer(['192.168.1.10:9000', '10.0.0.10:9000']);
foreach ($server->receive() as $peer => [$data, $socket]) {
// $socket is the interface that received the packet
// Reply will have the correct source IP
stream_socket_sendto($socket, "Reply", 0, $peer);
}