2017 © Pedro Peláez
 

library http

An http library developed for the laravel framework. aliases itself as HttpClient

image

vinelab/http

An http library developed for the laravel framework. aliases itself as HttpClient

  • Tuesday, October 24, 2017
  • by Vinelab
  • Repository
  • 10 Watchers
  • 53 Stars
  • 100,676 Installations
  • PHP
  • 11 Dependents
  • 0 Suggesters
  • 31 Forks
  • 5 Open issues
  • 24 Versions
  • 4 % Grown

The README.md

build status, (*1)

Dependency Status, (*2)

SensioLabsInsight, (*3)

http://Client

A smart, simple and fault-tolerant HTTP client for sending and recieving JSON and XML., (*4)

Installation

Composer

composer require vinelab/http, (*5)

// change this to point correctly according
// to your folder structure.
require './vendor/autoload.php';

use Vinelab\Http\Client as HttpClient;

$client = new HttpClient;

$response = $client->get('echo.jsontest.com/key/value/something/here');

var_dump($response->json());

Laravel

Edit app.php and add 'Vinelab\Http\HttpServiceProvider', to the 'providers' array., (*6)

It will automatically alias itself as HttpClient so no need to alias it in your app.php, unless you would like to customize it - in that case edit your 'aliases' in app.php adding 'MyHttp' => 'Vinelab\Http\Facades\Client',, (*7)

Usage

GET

Simple

$response = HttpClient::get('http://example.org');

// raw content
$response->content();

With Params

$request = [
    'url' => 'http://somehost.net/something',
    'params' => [

        'id'     => '12350ME1D',
        'lang'   => 'en-us',
        'format' => 'rss_200'
    ]
];

$response = HttpClient::get($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

POST

$request = [
    'url' => 'http://somehost.net/somewhere',
    'params' => [

        'id'     => '12350ME1D',
        'lang'   => 'en-us',
        'format' => 'rss_200'
    ]
];

$response = HttpClient::post($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

Options

These options work with all requests., (*8)

Timeout

You can set the timeout option in order to specify the number of seconds that your request will fail, if not completed., (*9)

$request = [
    'url' => 'http://somehost.net/somewhere',
    'params' => [

        'id'     => '12350ME1D',
        'lang'   => 'en-us',
        'format' => 'rss_200'
    ],
    'timeout' => 10
];

Headers

$response = HttpClient::get([
    'url' => 'http://somehost.net/somewhere',
    'headers' => ['Connection: close', 'Authorization: some-secret-here']
]);

// The full headers payload
$response->headers();

Basic Auth

$response = HttpClient::get([
    'url' => 'http://somehost.net/somewhere',
    'auth' => [
        'username' => 'user',
        'password' => 'pass'
    ],
    'params' => [
        'var1'     => 'value1',
        'var2'   => 'value2'
    ]
]);

Digest Auth

$response = HttpClient::get([
    'url' => 'http://some.where.url',
    'digest' => [
        'username' => 'user',
        'password' => 'pass'
    ],
    'params' => [
        'var1'     => 'value1',
        'var2'   => 'value2'
    ]
]);

Enforce HTTP Version

HttpClient::get(['version' => 1.1, 'url' => 'http://some.url']);

Raw Content

HttpClient::post(['url' => 'http://to.send.to', 'content' => 'Whatever content here may go!']);

Custom Query String

The content passed in the content key will be concatenated to the URL followed by a ?, (*10)

HttpClient::get(['url' => 'http://my.url', 'content' => 'a=b&c=d']);

It is pretty much the same process with different HTTP Verbs. Supports GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, (*11)

Fault Tolerance

Fault tolerance allows the request to be re-issued when it fails (i.e. timeout). This is useful in cases such as Microservices: When a service is down and is being called by another service, with fault tolerance the request will be re-issued in the hopes of the destination service being up again., (*12)

Issue a fault-tolerant request by setting the tolerant flag to true in the request. Also, specify the time it should wait until it tries again with timeUntilNextTry (in seconds) and the number of tries before it is considered a failure with triesUntilFailure (in seconds)., (*13)

$request = [
    'url' => 'http://somehost.net/somewhere',
    'params' => [

        'id'     => '12350ME1D',
        'lang'   => 'en-us',
        'format' => 'rss_200'
    ],
    'timeout' => 10
    'tolerant' => true,
    'timeUntilNextTry' => 1,
    'triesUntilFailure' => 3
];

In case of timeout occurance, a HttpClientRequestFailedException will be thrown., (*14)

IMPORTANT! Notice: In order to make use of fault tolerance option, you must specify the timeout parameter too., (*15)

The Versions

24/10 2017

dev-master

9999999-dev

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

24/10 2017

v1.5.4

1.5.4.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

30/01 2017

v1.5.3

1.5.3.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

12/12 2016

v1.5.2

1.5.2.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

05/12 2016

v1.5.1

1.5.1.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

24/11 2016

v1.5.0

1.5.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

23/11 2016

v1.4.0

1.4.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

02/11 2016

v1.3.0

1.3.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

26/02 2015

v1.2.0

1.2.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

27/10 2014

v1.1.1

1.1.1.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

26/10 2014

v1.1.0

1.1.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api curl json xml http client

19/08 2014

v1.0.0

1.0.0.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

19/08 2014

v0.1.10

0.1.10.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

27/03 2014

v0.1.9

0.1.9.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

16/12 2013

v0.1.8

0.1.8.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

16/12 2013

v0.1.7

0.1.7.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

28/07 2013

0.1.7

0.1.7.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

08/06 2013

v0.1.6

0.1.6.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

06/06 2013

v0.1.5

0.1.5.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

06/06 2013

v0.1.4

0.1.4.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

03/06 2013

v0.1.3

0.1.3.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

01/06 2013

v0.1.2

0.1.2.0

An http library developed for the laravel framework. aliases itself as HttpClient

  Sources   Download

MIT

The Requires

 

The Development Requires

30/05 2013

v0.1.1

0.1.1.0

An http library developed for the laravel framework. Populates itself as HttpClient (as alias)

  Sources   Download

MIT

The Requires

 

30/05 2013

v0.1.0

0.1.0.0

  Sources   Download

The Requires