Swoole\Http\Response->write()

Declaration

<?php
Swoole\Http\Response->write(string $data): bool

Parameters

data

Data to write to the HTTP body. The max length of the data by default is 2M. Controlled by buffer_output_size

Return

bool

If successful, true is returned otherwise false

Description

Send data back to the HTTP client in a segmented chunk, the write() method uses HTTP Chunk to send content back to the browser. This feature uses Transfer-Encoding to transfer segmented messages or payload body data between a client and server, data can be sent in a series of chunks.

If you have already performed calls to write() and then call $response->end() afterwards, it will send a chunk of length 0 to end data transmission with the client.

This method must be called before the $response->end() method.

Example

<?php

$server->on('Request', function(Swoole/Server/Request $request, Swoole/Server/Response $response)
{
  $response->write('Hello ');
  $response->write('World!');

  // Optional, but will set the content length to 0 and end the response
  $response->end();
});

Notes

  • If you want to send a complete HTTP body back to the client all at once, the $response->end method might be more useful.

  • Adjust max data size by changing buffer_output_size, the default is 2M