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
<?php OpenSwoole\Coroutine\Http\Client->set(array $options): bool
The key-value array of settings to use for the client.
Returns true
on successful and false
when not.
Update or set the settings of a HTTP Client.
This method allows you to set options on a per client basis, this client shares options with the other coroutine clients, please also checkout the overall settings under client configuration.
If there is no specific settings updated, the HTTP client uses the following default values (these are additional options for this client only):
method
: The HTTP method to use
reconnect
: How many times to retry to connect if no success
timeout
: The wait time for connection timeouts, this will enable HTTP timeout detection if set, the unit is seconds using a float type.
-1
.-2
.defer
: If to defer the current request or not
keep_alive
: If to enable keep_alive
to use persistent connections
websocket_mask
: If to enable Websocket mask, by default it is set to true
due to RFC regulations. However, this option will impact performance slightly but you may disable it if it is not needed. This option when true
will convert WebSocket data to use a mask instead.
websocket_compression
: If to compress Websocket messages or not. By default it is false
but when compression is used, performance will be slightly impacted due to having time for compressing data. It relies on the zlib
package and it must be installed. Compression only works if the server and client support it, compression support is determined during the handshake, see RFC-7692.
OpenSwoole\WebSocket\Server::WEBSOCKET_FLAG_COMPRESS
flag to enable compression for a specific connection, see WebSocket Compression example.http_compression
: If to compress the HTTP request or not.
Since v.4.7.0
body_decompression
: If to decompress the returned body or not.Since v.4.7.0
Client Configuration Defaults Example
<?php
$client->set([
'method' => 'GET',
'reconnect' => 1,
'timeout' => 0.5,
'defer' => false,
'keep_alive' => true,
'websocket_mask' => true,
'websocket_compression' => false,
'http_compression' => false,
'body_decompression' => true,
]);
<?php
use OpenSwoole\Coroutine\Http\Client;
co::run(function()
{
$client = new Client('127.0.0.1', 80);
// Setup the client here
$client->set([
'timeout' => 1,
'reconnect' => 3,
'keep_alive' => true,
]);
$client->setHeaders([
'Host' => 'localhost',
'User-Agent' => 'Chrome/49.0.2587.3',
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
]);
$client->get('/index.php');
echo $client->body;
$client->close();
});