Skip to content

Instantly share code, notes, and snippets.

@shin1x1
Created March 20, 2017 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shin1x1/ba1a41ae0a4d7a4fd4d65f32819d2d82 to your computer and use it in GitHub Desktop.
Save shin1x1/ba1a41ae0a4d7a4fd4d65f32819d2d82 to your computer and use it in GitHub Desktop.
<?php
const HOST = '49.212.25.40';
const PORT = '80';
function createStream() {
$errorNo = $errorMessage = null;
$stream = stream_socket_client(HOST . ':' . PORT, $errorNo, $errorMessage, 10);
if (!$stream) {
throw new \Exception($errorNo . ':', $errorMessage);
}
fwrite($stream, "GET /sleep.php HTTP/1.0\r\n\r\n");
return $stream;
}
$streams = [
createStream(),
createStream(),
createStream(),
];
$write = $except = [];
while (count($streams) > 0) {
$read = $streams;
$ret = stream_select($read, $write, $except, 10);
if ($ret === false) {
throw new \Exception('error');
} else if ($ret === 0) {
throw new \Exception('could not read');
} else {
foreach ($read as $r) {
$i = array_search($r, $streams);
echo fread($r, 4096);
fclose($r);
unset($streams[$i]);
}
}
}
@shin1x1
Copy link
Author

shin1x1 commented Mar 20, 2017

$ php non-blocking-http-client.php
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 20 Mar 2017 06:10:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: close

done
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 20 Mar 2017 06:10:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: close

done
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 20 Mar 2017 06:10:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: close

done

@ytake
Copy link

ytake commented Mar 20, 2017

いけそうな気がしてきました!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment