Skip to content

Instantly share code, notes, and snippets.

@volca
Created October 25, 2011 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save volca/1312803 to your computer and use it in GitHub Desktop.
Save volca/1312803 to your computer and use it in GitHub Desktop.
http request with libevent
<?php
function http_get($host) {
// create event base
$base_fd = event_base_new();
// create a new event
$event_fd = event_new();
// resource to be monitored
$fd = fsockopen($host, 80, $errno, $errstr, 30);
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fd, $out);
// set event on passed file name
event_set($event_fd, $fd, EV_WRITE | EV_PERSIST, function($fd, $events, $arg) {
echo fread($fd, 4096);
if(feof($fd)) {
fclose($fd);
event_base_loopexit($arg[1]);
echo "done";
}
}, array($event_fd, $base_fd));
// associate base with this event
event_base_set($event_fd, $base_fd);
// register event
event_add($event_fd);
// start event loop
event_base_loop($base_fd);
}
for($i = 0; $i < 2; $i++) {
http_get($argv[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment