Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
The Swoole\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.
Swoole\Coroutine\Http\Client::__construct
Swoole\Coroutine\Http\Client->set
Swoole\Coroutine\Http\Client->setMethod
Swoole\Coroutine\Http\Client->setHeaders
Swoole\Coroutine\Http\Client->setBasicAuth
Swoole\Coroutine\Http\Client->setCookies
Swoole\Coroutine\Http\Client->setData
Swoole\Coroutine\Http\Client->addData
Swoole\Coroutine\Http\Client->addFile
Swoole\Coroutine\Http\Client->setDefer
Swoole\Coroutine\Http\Client->getDefer
Swoole\Coroutine\Http\Client->execute
Swoole\Coroutine\Http\Client->get
Swoole\Coroutine\Http\Client->post
Swoole\Coroutine\Http\Client->upgrade
Swoole\Coroutine\Http\Client->push
Swoole\Coroutine\Http\Client->recv
Swoole\Coroutine\Http\Client->download
Swoole\Coroutine\Http\Client->getCookies
Swoole\Coroutine\Http\Client->getHeaders
Swoole\Coroutine\Http\Client->getHeaderOut
Swoole\Coroutine\Http\Client->getStatusCode
Swoole\Coroutine\Http\Client->getBody
Swoole\Coroutine\Http\Client->getsockname
Swoole\Coroutine\Http\Client->getpeername
Swoole\Coroutine\Http\Client->getPeerCert
Swoole\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 Swoole 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 Swoole\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.