EventHttpConnection::makeRequest

(PECL event >= 1.4.0-beta)

EventHttpConnection::makeRequest通过指定连接发出 HTTP 请求

描述

public EventHttpConnection::makeRequest( EventHttpRequest $req , int $type , string $uri ): bool

通过指定连接发出 HTTP 请求。 typeEventHttpRequest::CMD_* 常量之一。

参数

req

用于发送请求的连接对象。

type

EventHttpRequest::CMD_* 常量 之一。

uri

与请求关联的 URI。

返回值

成功时返回 true,失败时返回 false

示例

示例 #1 EventHttpConnection::makeRequest() 示例

<?php
function _request_handler($req, $base) {
echo
__FUNCTION__, PHP_EOL;

if (
is_null($req)) {
echo
"Timed out\n";
} else {
$response_code = $req->getResponseCode();

if (
$response_code == 0) {
echo
"Connection refused\n";
} elseif (
$response_code != 200) {
echo
"Unexpected response: $response_code\n";
} else {
echo
"Success: $response_code\n";
$buf = $req->getInputBuffer();
echo
"Body:\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

参见

添加备注

用户贡献的备注

此页面没有用户贡献的备注。
To Top