Skip to content

Instantly share code, notes, and snippets.

@davidecavaliere
Created October 15, 2014 10:01
Show Gist options
  • Save davidecavaliere/52ae9037c085d5f6bdbd to your computer and use it in GitHub Desktop.
Save davidecavaliere/52ae9037c085d5f6bdbd to your computer and use it in GitHub Desktop.
non blocking i/o php socket
$urls = ['www.amazon.com',...];
foreach ($url as $ip => $url) {
// create a stream that returns immediately
// STREAM_CLIENT_CONNECT _MUST_ be passed
// STREAM_CLIENT_ASYNC_CONNECT says create connection
// asyncrhronously
$socket = stream_socket_client("tcp://$ip:80", $errno, $errstr, 0,
STREAM_CLIENT_CONNECT, STREAM_CLIENT_ASYNC_CONNECT);
// set stream as non blockcing with 0, deafult is 1
stream_set_blocking($socket, 0);
$sockets[(int) $socket] = $socket;
$requests[(int) $socket] = $socket;
}
while (!empty($sockets)) {
// run the loop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment