Join 4,000+ others and never miss out on new tips, tutorials, and more.
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.
OpenSwoole\Coroutine\Http\Client::__construct
OpenSwoole\Coroutine\Http\Client->set
OpenSwoole\Coroutine\Http\Client->setMethod
OpenSwoole\Coroutine\Http\Client->setHeaders
OpenSwoole\Coroutine\Http\Client->setBasicAuth
OpenSwoole\Coroutine\Http\Client->setCookies
OpenSwoole\Coroutine\Http\Client->setData
OpenSwoole\Coroutine\Http\Client->addData
OpenSwoole\Coroutine\Http\Client->addFile
OpenSwoole\Coroutine\Http\Client->setDefer
OpenSwoole\Coroutine\Http\Client->getDefer
OpenSwoole\Coroutine\Http\Client->execute
OpenSwoole\Coroutine\Http\Client->get
OpenSwoole\Coroutine\Http\Client->post
OpenSwoole\Coroutine\Http\Client->upgrade
OpenSwoole\Coroutine\Http\Client->push
OpenSwoole\Coroutine\Http\Client->recv
OpenSwoole\Coroutine\Http\Client->download
OpenSwoole\Coroutine\Http\Client->getCookies
OpenSwoole\Coroutine\Http\Client->getHeaders
OpenSwoole\Coroutine\Http\Client->getHeaderOut
OpenSwoole\Coroutine\Http\Client->getStatusCode
OpenSwoole\Coroutine\Http\Client->getBody
OpenSwoole\Coroutine\Http\Client->getsockname
OpenSwoole\Coroutine\Http\Client->getpeername
OpenSwoole\Coroutine\Http\Client->getPeerCert
OpenSwoole\Coroutine\Http\Client->close
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.
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.
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
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.
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.
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.
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.
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.