OpenSwoole\Http\Response::create()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

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

Parameters

fd

The client connection $fd number

Return

OpenSwoole\Http\Response

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

Description

Construct a new OpenSwoole\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 OpenSwoole 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 OpenSwoole\Http\Server("127.0.0.1", 9501);

$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response)
{
    $response->detach();
    $response2 = OpenSwoole\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 OpenSwoole\Http\Server("127.0.0.1", 9501);

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

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

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

// Other event methods...

$server->start();
Last updated on September 1, 2022