PHPackages                             rnr1721/le7-api-request - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. rnr1721/le7-api-request

ActiveLibrary[Framework](/categories/framework)

rnr1721/le7-api-request
=======================

API client for le7 PHP MVC framework or any PSR project

1.1.1(2y ago)030MITPHPPHP &gt;=8.1

Since May 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/rnr1721/le7-api-request)[ Packagist](https://packagist.org/packages/rnr1721/le7-api-request)[ RSS](/packages/rnr1721-le7-api-request/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (13)Used By (0)

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
===================

[](#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:

What it can?
------------

[](#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
------------

[](#requirements)

- PHP 8.0 or higher
- Composer (for installing dependencies)

Installation
------------

[](#installation)

1. Install via composer:

```
composer require rnr1721/le7-api-request
```

Testing
-------

[](#testing)

```
composer test
```

Usage
-----

[](#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:

```
use Core\Factories\HttpClientFactory;
use Nyholm\Psr7\Factory\Psr17Factory;

// Create PSR factories. Nyholm realisation is a single factory to all
$psr17Factory = new Psr17Factory();

// Create httpClient (PSR ClientInterface implementation)
$httpClient = new HttpClientFactory($psr17Factory);

$factory = new HttpClientFactory(
    $psr17Factory, // UriFactoryInterface
    $psr17Factory, // RequestFactoryInterface
    $psr17Factory, // ResponseFactoryInterface
    $psr17Factory, // StreamFactoryInterface
    $httpClient // ClientInterface implementation
);

$apiRequest = $factory->getApiRequest()

$data = [
    // Request data here
];

$headers = [
    'content-language' => 'ru'
];

// Get ResponseInterface for POST request
$apiRequest->post('https://example.com/api', $data, $headers)->getResponse();

// Get array for GET request
$apiRequest->get('https://example.com/api')->toArray();

// Get object for PUT request
$apiRequest->put('https://example.com/api')->toObject();

// Get ResponseInterface for PUT request
// You can use request() method for any request
$apiRequest->request('PUT','https://example.com/api')->getResponse();

// Get array from response
$apiRequest->request('POST','https://example.com', $data, $headers)->toArray();

// Get object from response
$apiRequest->request('POST', 'https://example.com', $data, $headers)->toObject();

// This will return ResponseInterface of request
$apiRequest->getResponse('POST', 'https://example.com', $data, $headers);

// You can set Uri separately if you need it
$apiRequest->setUri('https://example.com');
$apiRequest->getResponse('POST', null, $data, $headers);
$apiRequest->get();

// Make something

// Get last created request
$apiRequest->getLast()->toArray();
```

Headers
-------

[](#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() &amp; setHeaders() methods. Now, you will see how setup global headers, that will be added for all requests:

```
$headers = [
    'My-Great-Header' => 'header_value'
    // Array with headers
]

// Set many headers for all requests in future
$apiRequest->setGlobalHeaders($headers);

// Set one global permanent header
$apiRequest->setGlobalHeader('Content-Language', 'en');
```

Also for one-time request headers:

```
$headers = [
    'My-Great-Header' => 'header_value'
    // Array with headers
]

// Set many headers for next request only
$apiRequest->setHeaders($headers);

// Set one header for next request only
$apiRequest->setHeader('Content-Language', 'en');

// Also you can set one-time header in method:
$apiRequest->get('https://example.com', null, $headers);
```

Sending files with multipart/form-data
--------------------------------------

[](#sending-files-with-multipartform-data)

```
$data = [
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'file' => new SplFileInfo('/path/to/file.jpg')
];

$apiRequest->setContentType('multipart/form-data');

$response = $apiRequest->post('/upload', $data);
```

Client setup
------------

[](#client-setup)

If you are using my implementation of ClientInterface,  you can setup some options of client:

```
$apiRequest->setTimeout(5); //default 10
$apiRequest->setMaxRedirects(5); // Default is 3
$apiRequest->setFollowLocation(false); // Default is true
```

JSON and form-data
------------------

[](#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:

```
// This is content type for requests,
// Default is application/json
$apiRequest->setContentType('multipart/form-data')
```

Another way is set header. It turn needle mode automatically

Convertors
----------

[](#convertors)

By default, you can convert recieved ResponseInterface to array or object like this:

```
// Get array from response
$apiRequest->request('POST', 'https://example.com', $data, $headers)->toArray();
```

```
// Get array from response
$apiRequest->request('POST', 'https://example.com', $data, $headers)->toObject();
```

Also, you can write own convertor. It must implement ResponseConvertorInterface:

```
