PHPackages                             gemz/http-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. gemz/http-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

gemz/http-client
================

Gemz Http Client is a simple Symfony Http-Client wrapper to provide an easy development experience for most use cases.

v2.3.1(6y ago)0211MITPHPPHP ^7.2.5CI failing

Since Feb 4Pushed 6y agoCompare

[ Source](https://github.com/gemzio/http-client)[ Packagist](https://packagist.org/packages/gemz/http-client)[ Docs](https://github.com/gemzio/http-client)[ RSS](/packages/gemz-http-client/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (7)Versions (11)Used By (1)

Gemz Http-Client
================

[](#gemz-http-client)

[![Latest Version on Packagist](https://camo.githubusercontent.com/50d395b4ff74b5f0d18e2adbc0c54c7986deb7a4e0a7cc20bcf7a9c680cf0b93/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67656d7a2f687474702d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gemz/http-client)[![GitHub Tests Action Status](https://camo.githubusercontent.com/abe65613f472399bbfda92cbb9d7f970904bfc1480b390998d916826e7c46df2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f67656d7a696f2f687474702d636c69656e742f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/gemzio/http-client/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Quality Score](https://camo.githubusercontent.com/2471399fe7d4f00ea33a119f90b5fb351b30024a9e14900a4b5860a875075435/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f67656d7a696f2f687474702d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/gemzio/http-client)[![Total Downloads](https://camo.githubusercontent.com/6df5a9220706ce341cfa836d4ba0452e474337e4baa91e2c9e005198021c84ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67656d7a2f687474702d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gemz/http-client)

Gemz Http Client is a thin [Symfony Http-Client](https://github.com/symfony/http-client) wrapper to provide an easy development experience for most use cases. Comes with easy to use asynchronous and concurrent requests.

If you need more functionality, just use [Guzzle](https://github.com/guzzle/guzzle) or [Symfony](https://github.com/symfony/http-client) clients.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require gemz/http-client
```

Basic Usage
-----------

[](#basic-usage)

```
use Gemz\HttpCLient\Client;

$client = Client::create();

// get request
$response = $client->get('https://myapi.com/users');

// json post request
$response = $client
    ->payload(['name' => 'John'])
    ->post('https://myapi.com/users');

// query url parameters
$response = $client
    ->queryParam('page', 20)
    ->get('https://myapi.com/users');
// calls https://myapi.com/users?page=20
```

Client Initialization
---------------------

[](#client-initialization)

You can configure the client with initial values. Configuration options are valid for all requests made with the client object unless you override the options in the request itself.

```
use Gemz\HttpCLient\Client;
use Gemz\HttpCLient\Config;

// Basic client with no config options
$client = Client::create();
$client = new Client();

// Client with config options
$config = Config::make()
    ->header('API-KEY', 'yourkey')
    ->baseUri('https://myapi.com');

$response = Client::create($config)->get('users');
```

> All possible options are identical in configuration and request. If you use the same option in config and the request, the request option will override the config option.

That is great when you have to use different options in some requests other than in the config.

```
$client = Client::create(Config::make()->header('X-KEY', 'yourkey'));

$response = $client->header('X-KEY', 'otherkey')->get('users');
```

Default Settings
----------------

[](#default-settings)

There are some default settings:

- Content-Type : 'application/json'
- All requests are made `asynchronous`
- Max Redirects 20
- Timeout defaults to ini\_get('default\_socket\_timeout')
- Max Duration 0 means unlimited

Configuration And Request Options
---------------------------------

[](#configuration-and-request-options)

```
// Authentication
$client->authBasic('username', 'password');
$client->authBearer('token');

// Headers
$client->header('key', 'value');
$client->headers(['key' => 'value']);

// User-Agent
$client->userAgent('userAgent');

// Query Parameters
$client->queryParam('key', 'value');
$client->queryParams(['key1' => 'value1']);
// will result in ...?key=value&key1=value1

// Timeout in seconds
$client->timeout(10);

// Max Redirects
// 0 means unlimited
$client->allowRedirects(3);
$client->doNotAllowRedirects();

// Max Request  Response Duration
// in seconds
$client->maxDuration(30);

// Throw errors if response status code is >= 400
$client->throwErrors();

// Content-Types
$client->contentType('contentType');
$client->asJson();
$client->asFormParams();
$client->asPlain();
$client->asMultipart();

// Multipart form data will automatically transformed in the correct format
// throws an exception if content-type and payload does not match
$client->payload(['key' => 'value']);
$client->payload('my message');

// payload for multipart
$client->payload([
    'key' => 'value',
    'file_field' => Client::fileHandler('pathToFile')
]);

// without verifing ssl
// Default is verifing
$client->doNotVerifySsl();
$client->verifySsl();

// Proxy
$client->useProxy('tcp://...');

// Methods
// Endpoint can also be an URI
$client->get('users/1');
$client->get('https://myapi.com/users/1');
$client->post('users');
$client->put('users/1');
$client->patch('users/1');
$client->head('users/1');
$client->delete('users/1');

// Setting Symfony Specific Client Options
$client->option('
