} // Event callback function eventcb($bev, $events, $base) { if ($events & EventBufferEvent::CONNECTED) { echo "Connected.\n"; } elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) { if ($events & EventBufferEvent::ERROR) { echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL; } echo "Closing\n"; $base->exit(); exit("Done\n"); } } if ($argc != 3) { echo <<<EOS Trivial HTTP 0.x client Syntax: php {$argv[0]} [hostname] [resource] Example: php {$argv[0]} www.google.com / EOS; exit; } $base = new EventBase(); $dns_base = new EventDnsBase($base, TRUE); $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", null, "eventcb", $base); $bev->enable(Event::READ | Event::WRITE); $bev->getOutput()->add("GET {$argv[2]} HTTP/1.0\r\n" . "Host: {$argv[1]}\r\n" . "Connection: Close\r\n\r\n"); $bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC); $base->dispatch();
} // Event callback function eventcb($bev, $stat, $base) { var_dump('status: ' . $stat); if ($stat & EventBufferEvent::CONNECTED) { echo "Connected.\n"; } elseif ($stat & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) { if ($stat & EventBufferEvent::ERROR) { echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL; } echo "Closing\n"; $base->exit(); exit("Done\n"); } elseif ($stat & EventBufferEvent::TIMEOUT) { echo "Timeout\n"; } } function writecb($bev, $base) { echo "writecb\r\n"; $bev->output->add(microtime(true) . "\r\n"); } $base = new EventBase(); $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::TIMEOUT, "readcb", null, "eventcb", $base); //"readcb", "writecb", "eventcb", $base); $bev->enable(Event::READ | Event::WRITE); $bev->getOutput()->add(microtime(true) . "\r\n"); $bev->setTimeouts(2, 2); $bev->connect('127.0.0.1:8000'); $base->dispatch();