2017 © Pedro Peláez
 

library httpclient

A smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options.

image

elfsundae/httpclient

A smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options.

  • Tuesday, January 16, 2018
  • by ElfSundae
  • Repository
  • 2 Watchers
  • 0 Stars
  • 503 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

HTTP Client

Latest Version on Packagist Software License tests StyleCI SymfonyInsight Grade Quality Score Code Coverage Total Downloads, (*1)

HttpClient is a smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options., (*2)

Installation

$ composer require elfsundae/httpclient

Usage

use ElfSundae\HttpClient;

Fetching Response Content

$html = (new HttpClient)->fetchContent('http://httpbin.org');

$data = (new HttpClient)->fetchJson('https://httpbin.org/ip');

Making Requests

$client = HttpClient::create('https://httpbin.org')
    ->catchExceptions(true)
    ->httpErrors(false)
    ->auth(['user', 'passwd']);

$query = $client->query(['foo' => 'bar'])->getJson('/get');

$form = $client->formParams(['foo' => 'bar'])->postJson('/post');

$json = $client->json(['foo' => 'bar'])->putJson('/put');

$download = $client->saveTo('image.png')->get('/image/png');

$file = fopen('image.png', 'r');
$uploadBody = $client->body($file)->postJson('/post');

$multipart = [
    'foo' => 'bar',
    'file' => $file,
    'image' => [
        'contents' => fopen('image.png', 'r'),
        'filename' => 'filename.png',
    ],
];
$formData = $client->multipart($multipart)->postJson('/post');

Async Requests

$promise = $client->json($data)->getAsync('/get');

$promise = $client->formParams($data)->postAsync('/post');

Applying Request Options

Using the option method:, (*3)

$client
    ->option('cert', $cert)
    ->option([
        'debug' => true,
        'headers.Content-Type' => 'application/json',
    ]);

Or using camelCase of any option name as a method on the client:, (*4)

$client
    ->allowRedirects(false)
    ->timeout(20)
    ->cookies($cookieJar)
    ->headers([
        'X-Foo' => 'foo',
    ]);

In addition, you may use header, accept, acceptJson, userAgent or contentType to set request headers:, (*5)

$client
    ->header('X-Foo', 'foo');
    ->header('X-Bar', 'bar');
    ->acceptJson()
    ->contentType('text/plain')
    ->userAgent('HttpClient/2.0')

Global Default Request Options

The static setDefaultOptions method can be used to configure default options for every new client instance:, (*6)

HttpClient::setDefaultOptions([
    'catch_exceptions' => true,
    'http_errors' => false,
    'connect_timeout' => 5,
    'timeout' => 20,
    'headers.User-Agent' => 'HttpClient/2.0',
]);

Catching Guzzle Exceptions

The catchExceptions method determines whether to catch Guzzle exceptions or not., (*7)

$response = $client->catchExceptions(true)->get('/api/path');

try {
    $response = $client->catchExceptions(false)->get('/api/path');
} catch (Exception $e) {
    // ...
}

Testing

$ composer test

License

This package is open-sourced software licensed under the MIT License., (*8)

The Versions

16/01 2018

dev-master

9999999-dev https://github.com/ElfSundae/httpclient

A smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options.

  Sources   Download

MIT

The Requires

 

The Development Requires

http client guzzle chaining httpclient

16/01 2018

2.0.0

2.0.0.0 https://github.com/ElfSundae/httpclient

A smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options.

  Sources   Download

MIT

The Requires

 

The Development Requires

http client guzzle chaining

15/01 2018

dev-doc

dev-doc https://github.com/ElfSundae/httpclient

A smart Guzzle wrapper provides convenient method chaining, global request options, and magic methods to customize request options.

  Sources   Download

MIT

The Requires

 

The Development Requires

http client guzzle chaining

11/12 2017

1.1.1

1.1.1.0 https://github.com/ElfSundae/httpclient

A wrapper of Guzzle HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

httpclient

18/06 2017

1.1.0

1.1.0.0 https://github.com/ElfSundae/httpclient

A wrapper of Guzzle HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

httpclient

14/06 2017

1.0.0

1.0.0.0 https://github.com/ElfSundae/httpclient

A wrapper of Guzzle HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

httpclient