🕌
【PHP】HTTP/1 クライアントで socket_select を使う
<?php
$address = '0.0.0.0';
$port = 8000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($sock);
socket_connect($sock, $address, $port);
socket_write($sock, "GET / HTTP/1.1\r\n\r\n");
$read = [$sock];
while (true) {
$ready = socket_select($read, $write, $except, 10);
if ($ready == 0) {
continue;
}
foreach($read as $read_sock) {
$input = socket_read($read_sock, 1024);
if (empty($input)) {
$key = array_search($read_sock, $read);
unset($read[$key]);
continue;
}
print($input);
}
if (!count($read)) {
break;
}
}
socket_close($sock);
Discussion