Download the PHP package rnr1721/le7-api-request without Composer
On this page you can find all versions of the php package rnr1721/le7-api-request. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package le7-api-request
le7-api-request
API client for le7 PHP MVC framework or any PSR project This is an simple PSR API client implementation
API Request Utility
This project provides a simple utility for simple making HTTP requests to an API. It includes a factory class for creating instances of the API request utility.
It not have own PSR ClientInterface implementation, and you can use any own. For example: https://github.com/rnr1721/le7-http-client
What it can?
- Create requests using any API, creating GET, POST, PUT, DELETE requests
- Allow to get ResponseInterface, array or object from response
- Built-in converters of JSON, XML and CSV responses to object or array
- Allow to create own convertors from ResponseInterface to your data
- Allow pre-define settings for run using DI containers
- Using different httpClients (ClientInterface), swithcing between them
- Get last request data
- Using events, for example for logging requests
- Set global headers and headers per-request
- Full PSR compatible
Requirements
- PHP 8.0 or higher
- Composer (for installing dependencies)
Installation
- Install via composer:
Testing
Usage
In this example, I use Nyholm PSR library, but you can use any, Guzzle for example. Also, you will need the ClientInterface implementation. I use this implementation: https://github.com/rnr1721/le7-http-client
Headers
You can use global headers, and headers for each request. Headers for each request you can inject in request methods when you call it or by setHeader() & setHeaders() methods. Now, you will see how setup global headers, that will be added for all requests:
Also for one-time request headers:
Sending files with multipart/form-data
Client setup
If you are using my implementation of ClientInterface, https://github.com/rnr1721/le7-http-client you can setup some options of client:
JSON and form-data
You can create these content-types of requests:
- application/json
- application/x-www-form-urlencoded
- multipart/form-data
By default is json, but you can switch to form-data:
Another way is set header. It turn needle mode automatically
Convertors
By default, you can convert recieved ResponseInterface to array or object like this:
Also, you can write own convertor. It must implement ResponseConvertorInterface:
As examples you can see ResponseArrayConverter and ResponseObjectConverter
Also, you can inject convertor when create ApiRequest instance:
Multiple HTTP clients
You can use different ClientInterface in different situations. Any client have own key. When you create instance of ApiRequest, ClientInterface that you inject have key 'default'. For some reasons you may need add ClientInterface instance. So, you can make this:
Uri prefixes
You can set the Uri prefix to add before all url in two way
When creating ApiRequest instance:
When you have already created instance
Container configuration
In this example I using these components, but you can use any others:
- Tobias Nyholm PSR-message realisation https://github.com/Nyholm/psr7
- PHP-Di dependency injection container https://php-di.org/
- My realisation of ClientInterface https://github.com/rnr1721/le7-http-client
- Optional: some PSR Event Dispatcher if need logging or something else
PSR Events
You can log requests or somehow else use event AfterApiRequestEvent:
All versions of le7-api-request with dependencies
psr/http-client Version ^1.0
psr/http-factory Version ^1.0
psr/http-message Version ^2.0
psr/event-dispatcher Version ^1.0