Swoole\Http\Response::create()

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Http\Response::create(int $fd): Swoole\Http\Response

Parameters

fd

The client connection $fd number

Return

Swoole\Http\Response

If successful, a newly created Swoole response object is returned otherwise false

Description

Construct a new Swoole\Http\Response object from a client connection $fd. This method is usually used for when you are detaching a response.

You use this method to reconstruct a Swoole HTTP response object after it was detached, allowing you to use it as normal, kind of reattaching the response again after it has been passed around to a Task Worker for example.

Make sure the response object has been detached before calling this method otherwise the server may send back the same request twice.

Example

<?php

$server = new Swoole\Http\Server("127.0.0.1", 9501);

$server->on('Request', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
{
    $response->detach();
    $response2 = Swoole\Http\Response::create($response->fd);
    $response2->end("Hello World!");
});

// Other event methods...

$server->start();

Or you could temporarily detach to use the $server methods:

<?php

$server = new Swoole\Http\Server("127.0.0.1", 9501);

$server->on('Request', function(Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server)
{
    $response->detach();

    $server->...($response->fd);

    $response2 = Swoole\Http\Response::create($response->fd);
    $response2->end("Hello World!");
});

// Other event methods...

$server->start();
Last updated on August 31, 2022