moccalotto / hayttp
Easy HTTP API requests with a fluent API
Requires
- php: >=7.0.0
Requires (Dev)
- ext-curl: *
- friendsofphp/php-cs-fixer: ^1.11
- phpspec/phpspec: ^3.4
- phpunit/phpunit: ^6.2
README
HTTP request made easy!
- 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:
{ "require": { "moccalotto/hayttp": "~1.0" } }
Alternatively execute the following command in your shell.
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.
Making GET Requests:
$response = Hayttp::get($url)->send();
A more interesting POST example.
$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.
$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.
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:
$stdClass = Hayttp::get($url) ->acceptJson() ->send() ->jsonDecoded();
Here is an example of a response decoded into a SimpleXmlElement
:
$simpleXmlElement = Hayttp::get($url) ->acceptXml() ->send() ->xmlDecoded();
Decode a url-encoded string into an associative array:
$array = Hayttp::get($url) ->send() ->urlDecoded();
Decode the respose, inferring the data type from the Content-Type header:
$variable = Hayttp::get($url)->send()->decoded();
Helper function
You can use the global hayttp
method to access the default hayttp instance.
$body = hayttp()->withTimeout(10) ->post($url) ->ensureJson() ->sendJson(['foo' => 'bar',]) ->decded();
You can also use the hayttp_*
method to make instant requests.
// 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:
// 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
// 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
// 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');