Skip to content

Instantly share code, notes, and snippets.

@m6w6
Created August 2, 2013 20:07
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 m6w6/6143009 to your computer and use it in GitHub Desktop.
Save m6w6/6143009 to your computer and use it in GitHub Desktop.
<?php
$async = new AsyncHttp;
for ($i = 1; $i < $argc; ++$i) {
$async->attach(new HttpRequest($argv[$i]), function ($req) use (&$state) {
$state = "";
printf("\n%d < %s\n", $req->getResponseCode(), $req->getUrl());
});
}
while ($async()) {
printf("\r... doing some other work%s", $state .= ".");
}
class AsyncHttp extends HttpRequestPool {
protected $callbacks;
function __construct() {
parent::__construct();
$this->callbacks = new SplObjectStorage;
}
function attach(HttpRequest $req, callable $cb) {
parent::attach($req);
$this->callbacks[$req] = $cb;
}
function __invoke() {
try {
$this->socketSelect(1);
$this->socketPerform();
} catch (Exception $e) {
echo $e;
return false;
}
foreach ($this->getFinishedRequests() as $req) {
$this->detach($req);
call_user_func($this->callbacks[$req], $req);
}
return count($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment