2017 © Pedro Peláez
 

library hayttp

Easy HTTP API requests with a fluent API

image

moccalotto/hayttp

Easy HTTP API requests with a fluent API

  • Sunday, July 29, 2018
  • by moccalotto
  • Repository
  • 1 Watchers
  • 0 Stars
  • 240 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 23 Versions
  • 0 % Grown

The README.md

Hayttp

Build Status, (*1)

HTTP request made easy!, (*2)

  • Lightweight, fast and small footprint.
  • Syntacticly sweet, easy and intuitive.
  • Short-hands to the 7 RESTful HTTP methods.
  • Real file (and blob) uploads.
  • Basic Auth.
  • Immutable.
  • Awesome advanced options:
    • Choose between CURL and php native http streams.
    • Create your own http transport engine (for instance a guzzle wrapper).
    • Choose ssl/tls scheme and version.
    • Create custom payloads.

Installation

To add this package as a local, per-project dependency to your project, simply add a dependency on moccalotto/hayttp to your project's composer.json file like so:, (*3)

{
    "require": {
        "moccalotto/hayttp": "~1.0"
    }
}

Alternatively execute the following command in your shell., (*4)

composer require moccalotto/hayttp

Usage

use Hayttp\Hayttp;

$response = Hayttp::get($url)->send();

REST Methods

Hayttp is essentially a factory that can create and initialize Request objects. It has methods for each of the 7 RESTful HTTP methods., (*5)

Making GET Requests:, (*6)

$response = Hayttp::get($url)->send();

A more interesting POST example., (*7)

$response = Hayttp::post($url)
    ->acceptJson()
    ->sendJson([
        'this' => 'associative',
        'array' => 'will',
        'be' => 'converted',
        'to' => 'a',
        'json' => 'object',
    ]);

A DELETE request that accept an XML body in the response., (*8)

$response = Hayttp::delete($url)
    ->acceptXml()
    ->send();

Decode responses

You can parse/unserialize the response payloads into native php data structures. Hayttp currently supports json, xml and rfc3986., (*9)

Below is an example of how parse a response as json. Json objects are converted to stdClass objects, and json arrays are converted to php arrays:, (*10)

$stdClass = Hayttp::get($url)
    ->acceptJson()
    ->send()
    ->jsonDecoded();

Here is an example of a response decoded into a SimpleXmlElement:, (*11)

$simpleXmlElement = Hayttp::get($url)
    ->acceptXml()
    ->send()
    ->xmlDecoded();

Decode a url-encoded string into an associative array:, (*12)

$array = Hayttp::get($url)
    ->send()
    ->urlDecoded();

Decode the respose, inferring the data type from the Content-Type header:, (*13)

$variable = Hayttp::get($url)->send()->decoded();

Helper function

You can use the global hayttp method to access the default hayttp instance., (*14)

$body = hayttp()->withTimeout(10)
    ->post($url)
    ->ensureJson()
    ->sendJson(['foo' => 'bar',])
    ->decded();

You can also use the hayttp_* method to make instant requests., (*15)

// All the calls below are equivalent

$response = hayttp_get($url);

$response = Hayttp::get($url)
                ->ensure2xx()
                ->send();

$response = hayttp()->get($url)
                ->ensure2xx()
                ->send();

Here are other examples of how to use the hayttp_* methods:, (*16)

// All the calls below are equivalent
$xml = new SimpleXmlElement('<root><groot>Boot</groot></root>');

$response = hayttp_post($url, $xml);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendXml($xml);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendXml($xml);

Posting json, (*17)

// All the calls below are equivalent
$data = ['foo' => ['bar' => ['baz']]];

$response = hayttp_post($url, $data);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendJson($data);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendJson($data);

Putting raw text, (*18)

// All the calls below are equivalent
$raw = file_get_contents($path);

$response = hayttp_put($url, $raw);

$response = Hayttp::put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');

$response = hayttp()->put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');

The Versions

29/07 2018

dev-master

9999999-dev https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

14/11 2017

1.0.1

1.0.1.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

28/09 2017

1.0.x-dev

1.0.9999999.9999999-dev https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

28/09 2017

1.0.0

1.0.0.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

15/07 2017

0.9.14

0.9.14.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

05/07 2017

0.9.13

0.9.13.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

03/07 2017

0.9.12

0.9.12.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

03/07 2017

09.11

09.11.0.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

03/07 2017

0.9.10

0.9.10.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

28/06 2017

0.9.8

0.9.8.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest http

28/06 2017

0.9.7

0.9.7.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest http

28/06 2017

0.9.6

0.9.6.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest http

28/06 2017

0.9.5

0.9.5.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest http

28/06 2017

0.9.4

0.9.4.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest http

24/06 2017

0.9.3

0.9.3.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

24/06 2017

0.9.2

0.9.2.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

03/05 2017

0.9.1

0.9.1.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

01/05 2017

0.9.0

0.9.0.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

04/11 2016

0.8.4

0.8.4.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

02/11 2016

0.8.3

0.8.3.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

02/11 2016

0.8.2

0.8.2.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

02/11 2016

0.8.1

0.8.1.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http

29/08 2016

0.8.0

0.8.0.0 https://moccalotto.github.io/docs/hayttp

Easy HTTP API requests with a fluent API

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

api rest http