Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
78 lines (54 sloc) 2.79 KB

Socket Client

The socket client uses the stream extension from PHP, which is integrated into the core.


  • TCP Socket Domain (tcp://hostname:port)
  • UNIX Socket Domain (unix:///path/to/socket.sock)
  • TLS / SSL encryption
  • Client Certificate (only for PHP > 5.6)


To install the Socket client, run:

$ composer require php-http/socket-client


The Socket client needs a :ref:`message factory <message-factory>` in order to to work:

use Http\Client\Socket\Client;

$options = [];
$client = new Client($messageFactory, $options);

The available options are:


Specify the remote socket where the library should send the request to

  • Can be a TCP remote: tcp://hostname:port
  • Can be a UNIX remote: unix:///path/to/remote.sock
  • Do not use a TLS/SSL scheme, this is handle by the SSL option.
  • If not set, the client will try to determine it from the request URI or host header.

Timeout in milliseconds for writing request and reading response on the remote


Activate or deactivate SSL/TLS encryption


Custom options for the context of the stream. See PHP stream context options.


Custom parameters for the context of the stream. See PHP stream context parameters.


When sending the request we need to buffer the body, this option specify the size of this buffer, default is 8192, if you are sending big file with your client it may be interesting to have a bigger value in order to increase performance.

As an example someone may want to pass a client certificate when using the ssl, a valid configuration for this use case would be:

use Http\Client\Socket\Client;

$options = [
    'stream_context_options' => [
        'ssl' => [
            'local_cert' => '/path/to/my/client-certificate.pem'
$client = new Client($messageFactory, $options);


This client assumes that the request is compliant with HTTP 2.0, 1.1 or 1.0 standard. So a request without a Host header, or with a body but without a Content-Length will certainly fail. To make sure all requests will be sent out correctly, we recommend to use the PluginClient with the following plugins:

  • ContentLengthPlugin sets the correct Content-Length header, or decorate the stream to use chunked encoding
  • DecoderPlugin decodes encoding coming from the response (chunked, gzip, deflate and compress)

:doc:`Read more on plugins </plugins/introduction>`