This library isn't in active development.
Please consider guzzlehttp/guzzle or another library instead.
Bug fixes only.
Hyper is an HTTP Client that aims to provide a simple, but powerful interface for making HTTP calls and fetching and manipulating API data.
Hyper::get('http://some/url').Hyper::make(...)->get('http://some/url').Response object which provides useful information like HTTP status code, body and headers.Response mixes in rexlabsarray-object which allows you to
easily interrogate API responses.Request via $response->getRequest().<?php
use RexlabsHyperHttpHyper;
$response = Hyper::get('http://openlibrary.org/subjects/love.json');
// The first book for 'love' is: Wuthering Heights
echo "The first book for '{$response->name}' is: {$response->works->first()->title}n";
echo "Total works: {$response->works->count()} booksn";To install in your project:
composer require rexlabs/hyper-httpThe RESTful methods all return a Response object which makes interacting with responses simple.
<?php
use RexlabsHyperHttpHyper;
$response = Hyper::get('https://example.com/url');
echo 'Status Code: '.$response->getStatusCode()."n";
echo (string)$response; // Output the response bodySince responses mixin ArrayObject you can easily fetch and manipulate values from the response:
<?php
use RexlabsHyperHttpHyper;
// Fetch historical price via CryptoCompare's public API for Ethereum
$response = Hyper::get('https://min-api.cryptocompare.com/data/pricehistorical', [
'fsym' => 'ETH',
'tsyms' => 'BTC,USD',
'ts' => '1452680400',
]);
// Output prices
printf("ETH->USD: %sn", $response->get('ETH.USD'));
printf("ETH->BTC: %sn", $response->get('ETH.BTC'));Use make() to simplify instantiation and then setup the object
for future requests:
<?php
use RexlabsHyperHttpHyper;
use RexlabsLoggerCustomLogger;
$hyper = Hyper::make()
->setBaseUri('http://example.com/api/v1')
->setHeader('X-App-Identity', 'Some App')
->setHeader('X-Correlation-Id', '12345')
->setLogger(new CustomLogger);$hyper = Hyper::make(array $config = [], GuzzleHttpClient $guzzle, PsrLogLoggerInterface $logger)To get complete control over instantiation, use the constructor and pass in a Guzzle instance:
<?php
use RexlabsHyperHttpClient;
use GuzzleHttpClient as GuzzleClient;
use PsrLogNullLogger;
$hyper = new Client(new GuzzleClient(), new NullLogger(), [
'base_uri' => 'http://example.com/api/v1',
'headers' => [
'X-App-Identity' => 'Some App',
],
]);
$response = $hyper->get('/messages');You can easily generate a cURL request for running from the command-line to reproduce your last request:
<?php
use RexlabsHyperHttpHyper;
echo Hyper::get('https://example.com/api/v1/resources')
->getCurlRequest();Output:
curl
'https://min-api.cryptocompare.com/data/pricehistorical?fsym=ETH&tsyms=BTC%2CUSD&ts=1452680400&extraParams=your_app_name'
-H 'Content-Type: application/json' -H 'Accept: application/json'Hyper provides the following methods for interacting with remote endpoints:
get(mixed $uri, array $query = [], array $headers = [], array $options = []): Response
Send an HTTP GET request, and return the Response:
$response = Hyper::get('https://example.com', ['sort' => 'latest'], ['X-Greeting' => 'Hello!']);
$response = $hyper->get('/v1/people');$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$query is an optional array of query parameters which will be appended to the uri.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.post(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
Send an HTTP POST request, and return the Response:
$response = Hyper::post('https://example.com/fruit', 'apples');
$response = $hyper->post('/v1/people', ['name' => 'Bob', 'age' => 25]);$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$body is the payload. If you provide an array, it will be converted and transported as json.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.Alternative methods:
$response = $hyper->postForm($uri, $formParams, $headers, $options);$response = $hyper->postMultipartForm($uri, $formParams, $headers, $options);put(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
Send an HTTP PUT request, and return the Response:
$response = Hyper::put('https://example.com/fruit', 'apples');
$response = $hyper->put('/v1/people', ['name' => 'Bob', 'age' => 25]);$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$body is the payload. If you provide an array, it will be converted and transported as json.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.patch(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
Send an HTTP PATCH request, and return the Response:
$response = Hyper::patch('https://example.com/fruit', 'apples');
$response = $hyper->patch('/v1/people', ['name' => 'Bob', 'age' => 25]);$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$body is the payload. If you provide an array, it will be converted and transported as json.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.delete(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
Send an HTTP DELETE request, and return the Response:
$response = Hyper::delete('https://example.com/fruit', 'apples');
$response = $hyper->delete('/v1/people/1');$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$body is the optional payload. If you provide an array, it will be converted and transported as json.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.call(string $method, mixed $uri, mixed $body, array $headers, array $options): Response
Send a generic HTTP request by specifying the method as the first argument.
// Statically
$response = Hyper::call('MOVE', 'myfile1234', ['new_location' => 'some_folder']);
// Http method verbs may also be invoked via method name
$response = Hyper::move('myfile1234', ['new_location' => 'some_folder']);
$response = Hyper::somethingelse(...);
// Via object
$response = $hyper->call('MOVE', 'myfile1234', ['new_location' => 'some_folder']);$method is the HTTP verb. Eg. GET or something not part of the standard.$uri is a string or a Uri. If the string is not absolute it will be appended to the base uri.$body is the optional payload. If you provide an array, it will be converted and transported as json.$headers is an optional array of headers (indexed by header name) that will be merged with any global headers.$options is an optional array of Guzzle client options.Methods available from the RexlabsHyperHttpMessageRequest object:
Return the UriInterface object which encapsulates the URI/URL for this request.
Return the HTTP method verb for this Request.
Retur an array of headers for this Request
Return a cURL request (string) suitable for running from the command-line. Useful for debugging requests.
Methods available from the RexlabsHyperHttpMessageResponse object:
Return the RexlabsHyperHttpMessageRequest object associated with the Response
Return a cURL request (string) suitable for running from the command-line. Useful for debugging requests.
Return the HTTP status code for this Response. EG. 200
Return the HTTP reason phrase associated with the status code. EG. "OK"
Returns true if this is a JSON response.
Converts a JSON response to an array and returns the array.
Converts a JSON response to an ArrayObject
Every Response object has all of the methods and functionality of the ArrayObject class from the rexlabsarray-object package.
This means based on the following response payload:
{
"books": [
{
"id": 1,
"title": "1984",
"author": "George Orwell"
},
{
"id": 2,
"title": "Pride and Prejudice",
"author": "Jane Austen"
}
]
}You can perform the following functions:
$response->books; // Instance of ArrayObject
$response->books->pluckArray('author'); // array [ 'George Orwell', 'Jane Austen' ]
$response->pluckArray('books.author'); // array [ 'George Orwell', 'Jane Austen' ]
$response->books->count(); // 2
$response->books->isCollection(); // true
$response->books[0]; // Instance of ArrayObject
$response->books[0]->isCollection(); // false
$response->books[0]->id; // 1
$response->get('books.1.title'); // "Pride and Prejudice"
foreach ($response->books as $book) {
echo "{$book->title} by {$book->author}n";
}You can also call:
$obj = $response->toObject(); // Instance of ArraybjectSet default config for all client's (defaults to [])
Hyper::setDefaultConfig($config);Set config for this client (values will override / merge with default)
$client = Hyper::make($config);Set the default logger used by all clients that don't provide one.
Must implement LoggerInterface (defaults to NullLogger)
Hyper::setDefaultLogger($logger);Log the curl string for all requests (requires a logger set)
$config = [
'log_curl' => true,
];Set the config passed to the underlying GuzzleClient
$config = [
'guzzle' => [
'verify' => false,
],
];
// Set for all clients
Hyper::setDefaultConfig($config);
// Set for one client
$client = Hyper::make($config);To run tests:
composer testsTo run coverage report:
composer coverageCoverage report is output to ./tests/report/index.html
Hyper allows extension for custom clients by:
MyHyperSubclass will return the correct instance created by MyHyperSubclassHyper will return the correct instance created by Hyperprotected static function makeClient to customise client class (eg replace new Client with new MyClient)protected static function makeConfig to customise default client configprotected static function makeGuzzleConfig to customise default guzzle clientprotected static function getBaseUri to provide a default base_uri to the clientContributions are welcome, please submit a pull-request or create an issue. Your submitted code should be formatted using PSR-1/PSR-2 standards.