略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: EventHttpRequest::__construct

2024-12-22

EventHttpRequest::__construct

(PECL event >= 1.4.0-beta)

EventHttpRequest::__constructConstructs EventHttpRequest object

说明

public EventHttpRequest::__construct( callable $callback , mixed $data = null )

Constructs EventHttpRequest object.

参数

callback

Gets invoked on requesting path. Should match the following prototype:

callback( EventHttpRequest $req = null , mixed $arg = null ): void
data

User custom data passed to the callback.

返回值

Returns EventHttpRequest object.

范例

示例 #1 EventHttpRequest::__construct() example

<?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 
$sPHP_EOL;
            }
        }
    }

    
$base->exit(NULL);
}


$address "127.0.0.1";
$port 80;

$base = new EventBase();
$conn = new EventHttpConnection($baseNULL$address$port);
$conn->setTimeout(5);
$req = new EventHttpRequest("_request_handler"$base);

$req->addHeader("Host"$addressEventHttpRequest::OUTPUT_HEADER);
$req->addHeader("Content-Length""0"EventHttpRequest::OUTPUT_HEADER);
$conn->makeRequest($reqEventHttpRequest::CMD_GET"/index.cphp");

$base->loop();
?>

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/eventhttprequest.construct.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3