Swoole\Coroutine\Http2\Client->read(...)

Declaration

<?php
Swoole\Coroutine\Http2\Client->read(float $timeout): Swoole\Http2\Response

Parameters

timeout

In seconds, the timeout of the request, 1.5 means 1.5 seconds. See the timeout guide link below.

Return

Returns a client Swoole\Http2\Response object when successful and false when not, check $client->errCode for more details upon an error.


Description

Receive multiple responses from the remote host.

When using this method, while waiting for IO Swoole will switch coroutines so resources are not wasted and return upon a response or failure.

The built in HTTP clients have a timeout option, allowing you to wait for a response, the timeout can be set in a few different ways, refer to the timeout guide for more information. However, the $timeout parameter will work in most cases.


Differences with recv()

The read() method is basically the same as recv() but the difference being, you can use read() to receive multiple responses from a stream. A successful response will always return Swoole\Http2\Response.

When receiving multiple read() responses, you must have used the $request->pipeline = true option so that a stream is kept open, allowing you to keep reading, see the write() method documentation for more details on using the pipeline option.

This method will always return a complete (whole) response, everything is read into memory first before returning.


Response HTTP/2 Object

This method returns a Swoole\Http2\Response object which is a class with no methods, only public class properties which you can access, see the response object documentation for more details and what it contains. This object is only returned on a successful response. Check $client->errCode if you receive false.