omitobisam / laravel-habitue
An Http client with the power of collections for your Jsonable requests
Requires
- php: >=7.4
- guzzlehttp/guzzle: ^6.3
- tightenco/collect: ^7.2
Requires (Dev)
- phpunit/phpunit: ~8.0
- dev-master
- v5.0-alpha
- v4.2.0
- v4.1.0
- v4.0.0
- v4.0-alpha
- v3.0.0
- v3.0-alpha
- 2.0.0
- v2.0-alpha
- 1.0.0
- v1.0-alpha-2
- v1.0-alpha-1
- v1.0-alpha
- dev-attempt-fix-to-laravel-collection-version-on-laravel-11-
- dev-attempt-fix-to-laravel-collection-version-on-laravel-11
- dev-Update-php-8.1+
- dev-Improve-package-descriptions
- dev-Use-github-actions
- dev-add-github-actions-for-test
- dev-Upgrade-to-PHP-8.0
- dev-feature/make-habitue-smarter
- dev-feature/allow-chaining-requests
- dev-v5.0-alpha
- dev-v4.0-alpha
- dev-v3.0-alpha
- dev-v2.0-alpha
- dev-v1.0-alpha
This package is auto-updated.
Last update: 2025-03-22 15:18:30 UTC
README
About Habitue
The easiest and fluent HTTP Client for PHP.
Installation
Using composer:
composer require omitobisam/laravel-habitue
or add to the require object of composer.json
file with the version number:
{ "require": { "omitobisam/laravel-habitue": "^v3.0" } }
After this run composer update
Usage
You can call the helper function simply:
$response = habitue('http://ninja.example/users')->get(); $response = habitue('http://ninja.example/users', ['data' => 'aaa'])->post(); $response = habitue('http://ninja.example/users')->delete(); $response = habitue('http://ninja.example/users', ['data' => 'aaa'])->patch(); $response->get('data')
You can call it simply statically:
use Habitue\Habitue; Habitue::::make('http://ninja.example/api/users')->get(); // or ->post()
With Configuration:
use Habitue\Habitue; // or simply Habitue::::make('http://ninja.example/api/users') // An instance of Habitue ->setBody(['page' => 2]) //set body ->setHeaders(['x-key' => 'abcd']) // set header(s) ->get()
Handling Responses returned:
use Habitue\Habitue; /** * @var $response Habitue\Integration\Collector */ $response = habitue('http://ninja.example/users')->get(); $response->statusCode(); // int $response->headers(); // array $response->toJson(); // string // And others method available in Illuminate\Collection $clientResponse = $response->response(); // Habitue\Integration\ClientResponse $clientResponse->getData(); // string $clientResponse->getStatusCode(); // int $clientResponse->getHeaders(); // array
The Response (as Habitue\Integration\Collector) is a smart Collection that provides all the methods available in Laravel Collection and helps to draw out values deeply nested into the response. Say your response is the following:
{ "name":"John Doe", "age":11, "height":57, "address": { "postal": { "code":"11111", "region":"lc" }, "city":"Tartu" } }
You can get the value code
with the following
use Habitue\Habitue; /** * @var $collected \Habitue\Integration\Collector */ $collected = \habitue('https://ninja.example/users')->get(); $collected->get('name'); //John Doe $collected->getName(); // John Doe $collected->getAddress() // Collection with {"postal": {"code":"11111","region":"lc"}, "city":"Tartu"} ->getPostal() // Collection with {"code":"11111","region":"lc"} ->getCode(); //11111
API Available
Habitue class
Habitue::__construct(url: string, [data: array = [...]], [config: array = [...]]) Habitue::setHeaders(headers: array, [overwrite: bool = false]): HabitueInterface Habitue::setBody(body: array, [overwrite: bool = false]): HabitueInterface Habitue::get([key = null]): HabitueResponse.getReturn): Collector|mixed // Depends on the type of configuration at config: 'habitue.return' Habitue::post([key = null]): Collector|mixed // Depends on the type of configuration at config: 'habitue.return' Habitue::patch([key = null]): Collector|mixed // Depends on the type of configuration at config: 'habitue.return' Habitue::put([key = null]): Collector|mixed // Depends on the type of configuration at config: 'habitue.return' Habitue::delete([key = null]): Collector|mixed // Depends on the type of configuration at config: 'habitue.return' Habitue::getResponse(): ClientResponse Habitue::make(url: string, [data: array = [...]], [config: array = [...]]): HabitueInterface
ClientResponse Class
ClientResponse::__construct(data: string, statusCode: int, headers: array) ClientResponse::getData(): string ClientResponse::getStatusCode(): int ClientResponse::getHeaders(): array ClientResponse::__toString(): string
Contributions
- Make a PR
- Make sure the tests passes
- It gets it approved
- It gets merged