|
| 1 | +# Socket Client for PHP HTTP |
| 2 | + |
| 3 | +[](https://github.com/php-http/socket-client/releases) |
| 4 | +[](LICENSE) |
| 5 | +[](https://travis-ci.org/php-http/socket-client) |
| 6 | +[](https://scrutinizer-ci.com/g/php-http/socket-client) |
| 7 | +[](https://scrutinizer-ci.com/g/php-http/socket-client) |
| 8 | +[](https://packagist.org/packages/php-http/socket-client) |
| 9 | + |
| 10 | +The socket client use the stream extension from PHP, which is integrated into the core. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +Via Composer |
| 15 | + |
| 16 | +``` bash |
| 17 | +$ composer require php-http/socket-client |
| 18 | +``` |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | + * TCP Socket Domain (tcp://hostname:port) |
| 23 | + * UNIX Socket Domain (unix:///path/to/socket.sock) |
| 24 | + * TLS / SSL Encyrption |
| 25 | + * Client Certificate (only for php > 5.6) |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +The SocketHttpClient class need a [message factory](https://github.com/php-http/message-factory) in order to work: |
| 30 | + |
| 31 | +```php |
| 32 | +$options = []; |
| 33 | +$client = new new Http\Socket\SocketHttpClient($messageFactory, $options); |
| 34 | +``` |
| 35 | + |
| 36 | +The `$options` array allow to configure the socket client. |
| 37 | + |
| 38 | +## Options |
| 39 | + |
| 40 | +Here is the list of available options: |
| 41 | + |
| 42 | + * remote_socket: Specify the remote socket where the library should send the request to |
| 43 | + |
| 44 | + Can be a tcp remote : tcp://hostname:port |
| 45 | + Can be a unix remote : unix://hostname:port |
| 46 | + |
| 47 | + Do not use a tls / ssl scheme, this is handle by the ssl option. |
| 48 | + If not set, the client will try to determine it from the request uri or host header. |
| 49 | + |
| 50 | + * timeout : Timeout for writing request or reading response on the remote |
| 51 | + * ssl : Activate or deactivate the ssl / tls encryption |
| 52 | + * stream_context_options : Custom options for the context of the stream, same as [PHP stream context options](http://php.net/manual/en/context.php) |
| 53 | + |
| 54 | + As an example someone may want to pass a client certificate when using the ssl, a valid configuration for this |
| 55 | + use case would be: |
| 56 | + |
| 57 | + ```php |
| 58 | + $options = [ |
| 59 | + 'stream_context_options' => [ |
| 60 | + 'ssl' => [ |
| 61 | + 'local_cert' => '/path/to/my/client-certificate.pem' |
| 62 | + ] |
| 63 | + ] |
| 64 | + ] |
| 65 | + $client = new Http\Socket\SocketHttpClient($messageFactory, $options); |
| 66 | + ``` |
| 67 | + |
| 68 | + * stream_context_params : Custom parameters for the context of the stream, same as [PHP stream context parameters](http://php.net/manual/en/context.params.php) |
| 69 | + * write_buffer_size : When sending the request we need to bufferize the body, this option specify the size of this buffer, default is 8192, |
| 70 | + if you are sending big file with your client it may be interesting to have a bigger value in order to increase performance. |
| 71 | + |
| 72 | +## Testing |
| 73 | + |
| 74 | +First launch the http server: |
| 75 | + |
| 76 | +```bash |
| 77 | +$ ./vendor/bin/http_test_server > /dev/null 2>&1 & |
| 78 | +``` |
| 79 | + |
| 80 | +Then the test suite: |
| 81 | + |
| 82 | +``` bash |
| 83 | +$ composer test |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +## Contributing |
| 88 | + |
| 89 | +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. |
| 90 | + |
| 91 | + |
| 92 | +## Security |
| 93 | + |
| 94 | +If you discover any security related issues, please contact us at [[email protected]](mailto:[email protected]). |
| 95 | + |
| 96 | + |
| 97 | +## License |
| 98 | + |
| 99 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments