Open Swoole Coroutine HTTP Client

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

The OpenSwoole\Coroutine\HTTP\Client supports HTTP 1.1, HTTPS and WebSocket protocols. It has to be executed within Coroutine context, inside a co::run(). This HTTP client is a pure C++ implementation and has a focus on high performance.

Multiple HTTP requests can be executed concurrently within one process, it supports Http-Chunk, Keep-Alive and the Form-Data format.

It has support for compression with gzip but you must have zlib installed to use it.

It is normally not recommended to use this client except you are looking for PHP WebSocket Client — you can use Open Swoole CURL within a coroutine context. This is supported due to coroutine hooks. Because there is full support for CURL in PHP when using Swoole, you are also able to use any libraries that are based on CURL in Swoole, for example like the GuzzlePHP HTTP Client.


Methods


Client Configuration

This HTTP client has general options which are shared between the others (TCP/UDP and HTTP2), please refer to the general client configuration first. Then you should read the specific configuration options. But they all use the same method to set these options.


Client Timeouts

All network requests (establish a connection, send data and receive data) may time out. This client has a few ways for how you can setup timeouts.

As each OpenSwoole client is written as a built-in class using coroutines, their timeouts are set the same way, refer to the timeout guide to understand how to setup timeouts.


Helper Functions

For convenience the Coroutine\Http\Client comes with three helper functions to make it easier and quicker to make simple HTTP requests.

Available since v4.6.4

request()

Initiate a HTTP request, specify the URL, method and if needed, data:

<?php
function request(string $url, string $method, $data = null, array $options = null, array $headers = null, array $cookies = null)

This method also allows you to specify client $options just as you would when using the client object and headers and cookies.

post()

Initiate a HTTP POST request, specifying the URL and data:

<?php
function post(string $url, array|string $data, array $options = null, array $headers = null, array $cookies = null)

When passing data it can be either a string or an array, using an array will cause the Content-Type format x-www-form-urlencoded to be used. Then there is optional client $options and headers and cookies.

get()

Initiate a HTTP GET request, specifying the URL:

<?php
function get(string $url, array $options = null, array $headers = null, array $cookies = null)

A GET request only requires the URL, you then have client $options and headers and cookies.

Full Helper Function Example

The helper request functions allow you to make one off HTTP requests quickly and easily, they are useful for when you don't need the full power of the HTTP client, checkout the full example to learn more.


Error Codes

When using this client, you are bound to come across processing errors. This mostly happens when using connect, send, recv and close methods. Most of these methods return false when there is a problem but you can use OpenSwoole\Coroutine\Http\Client->errCode to check for what went wrong.

Once you have the error code, you can convert it into a message:

<?php
echo socket_strerror($client->errCode);

// Or

echo $client->errMsg;

This will give you a Linux system error message.

For example, 111 will indicate that the client was refused and 110 means the client was timed out.

Last updated on September 1, 2022