Beispiel #1
0
}
// 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();
    } 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");
    }
}
$base = new EventBase();
$dns_base = new EventDnsBase($base, TRUE);
// We'll use async DNS resolving
if (!$dns_base) {
    exit("Failed to init DNS Base\n");
}
$bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", NULL, "eventcb", $base);
if (!$bev) {
    exit("Failed creating bufferevent socket\n");
}
//$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);
$output = $bev->output;
//$bev->getOutput();
if (!$output->add("GET  HTTP/1.0\r\n")) {
    exit("Failed adding request to output buffer\n");
}
// mole.eyousop.com:8538
if (!$bev->connectHost($dns_base, 'mole.eyousop.com', 8538, EventUtil::AF_UNSPEC)) {
    exit("Can't connect to host {$argv[1]}\n");
}
$base->dispatch();