(PECL event >= 1.4.0-beta)
EventHttpConnection::makeRequest — 通过指定的连接发出 HTTP 请求
通过指定的连接发出 HTTP 请求。type
是 EventHttpRequest::CMD_*
常量之一。
req
要通过其发送请求的连接对象。
type
uri
与请求关联的 URI。
示例 #1 EventHttpConnection::makeRequest() 例子
<?php
function _request_handler($req, $base) {
echo __FUNCTION__, PHP_EOL;
if (is_null($req)) {
echo "超时\n";
} else {
$response_code = $req->getResponseCode();
if ($response_code == 0) {
echo "连接被拒绝\n";
} elseif ($response_code != 200) {
echo "意外响应:$response_code\n";
} else {
echo "成功:$response_code\n";
$buf = $req->getInputBuffer();
echo "主体:\n";
while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
echo $s, PHP_EOL;
}
}
}
$base->exit(NULL);
}
$address = "127.0.0.1";
$port = 80;
$base = new EventBase();
$conn = new EventHttpConnection($base, NULL, $address, $port);
$conn->setTimeout(5);
$req = new EventHttpRequest("_request_handler", $base);
$req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER);
$req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER);
$conn->makeRequest($req, EventHttpRequest::CMD_GET, "/index.cphp");
$base->loop();
?>
以上示例将输出类似以下内容
_request_handler Success: 200 Body: PHP, date: 2013-03-13T20:27:52+05:00