1 # http\Client\Curl\User Examples
3 ## Example using the [pecl/ev](http://pecl.php.net/ev) loop:
7 class UserHandler implements http\Client\Curl\User
15 function __construct(http\Client $client) {
16 $this->client = $client;
19 function init(callable $run) {
23 function timer(int $timeout_ms) {
25 if (isset($this->timeout)) {
26 $this->timeout->set($timeout_ms/1000, 0);
27 $this->timeout->start();
29 $this->timeout = new EvTimer($timeout_ms/1000, 0, function() {
30 if (!call_user_func($this->run, $this->client)) {
32 $this->timeout->stop();
33 $this->timeout = null;
40 function socket($socket, int $action) {
46 case self::POLL_REMOVE:
47 if (isset($this->ios[(int) $socket])) {
49 $this->ios[(int) $socket]->stop();
50 unset($this->ios[(int) $socket]);
56 if ($action & self::POLL_IN) {
59 if ($action & self::POLL_OUT) {
62 if (isset($this->ios[(int) $socket])) {
63 $this->ios[(int) $socket]->set($socket, $ev);
65 $this->ios[(int) $socket] = new EvIo($socket, $ev, function($watcher, $events) use($socket) {
67 if ($events & Ev::READ) {
68 $action |= self::POLL_IN;
70 if ($events & Ev::WRITE) {
71 $action |= self::POLL_OUT;
73 if (!call_user_func($this->run, $this->client, $socket, $action)) {
75 $this->timeout->stop();
76 $this->timeout = null;
86 throw new BadMethodCallException("this example uses Ev::run()");
89 function wait(int $timeout_ms = null) {
90 throw new BadMethodCallException("this example uses Ev::run()");
94 throw new BadMethodCallException("this example uses Ev::run()");
98 $client = new http\Client;
100 "use_eventloop" => new UserHandler($client)
102 $client->enqueue(new http\Client\Request("GET", "http://example.com/"), function($r) {
103 var_dump($r->getResponseCode());
112 TTTTTTTTSTSTSUint(200)