Created
October 25, 2011 13:57
-
-
Save volca/1312803 to your computer and use it in GitHub Desktop.
http request with libevent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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