Simple PSR-18 HTTP Client.
- PHP >= 7.1
Using Composer:
$ composer require davidecesarano/embryo-client
You may use the get
, post
, put
, patch
, and delete
methods provided by the Http Client factory. These methods returns an instance of Psr\Http\Message\ResponseInterface
.
use Embryo\Http\Client\ClientFactory;
$http = new ClientFactory;
$response = $http->get('https://example.com/');
When making POST
, PUT
, and PATCH
requests you may send additional data to request with an array of data as their second argument.
$response = $http->post('https://example.com/users', [
'name' => 'Davide',
'surname' => 'Cesarano'
]);
Headers may be added to requests using the withHeaders
method. This method accepts an array of key / value pairs:
$response = $http
->withHeaders([
'X-First' => 'foo',
'X-Second' => 'bar'
])
->post('https://example.com/users', [
'name' => 'Davide',
'surname' => 'Cesarano'
]);
If you would like to quickly add a bearer token to the request's Authorization
header, you may use the withToken
method:
$response = $http
->withToken('token')
->post('https://example.com/users', [
'name' => 'Davide',
'surname' => 'Cesarano'
]);
You may use the withJson
method if you want to provide a raw request body when making a request:
$response = $http
->withJson()
->post('https://example.com/users', [
'name' => 'Davide',
'surname' => 'Cesarano'
]);
If you want to send files, you may use the withFile
method. This method accepts the name of the field and file absolute path:
$response = $http
->withFile('file1', '/path/to/file1')
->withFile('file2', '/path/to/file2')
->post('https://example.com/upload');
With timeout
method you may to specify the maximum number of seconds to wait for a response:
$response = $http
->withTimeout(30)
->get('https://example.com/users');