easy-http/symfony-layer

Http layer for Symfony Client

v0.3.0 2022-08-26 19:03 UTC

This package is auto-updated.

Last update: 2024-04-27 17:42:41 UTC


README

68747470733a2f2f626c6f672e706c656574732e6f72672f696d672f61727469636c65732f656173792d687474702d6c6f676f2d3332302e706e67

Build Status Code Quality Code Coverage

Symfony Layer

This is an HTTP layer for Symfony Client. For more layers see Easy Http.

Bugs Bugs Bugs

This library supports the following versions of Symfony Http Client.

  • Symfony Http Client v5.0 or later

Installation

Use following command to install this library:

composer require easy-http/symfony-layer

Usage

Simple requests

You can execute a simple request as follows.

use EasyHttp\SymfonyLayer\SymfonyClient;

$client = new SymfonyClient();
$response = $client->call('GET', 'https://api.ratesapi.io/api/2020-07-24/?base=USD');

$response->getStatusCode(); // 200
$response->parseJson();     // array

Prepared requests

A prepared request is a more flexible way to generate a request. You can use the setQuery method to specify request query.

use EasyHttp\SymfonyLayer\SymfonyClient;

$client = new SymfonyClient();

$client->prepareRequest('POST', 'https://api.ratesapi.io/api/2020-07-24/');
$client->getRequest()->setQuery(['base' => 'USD']);
$response = $client->execute();

$response->getStatusCode(); // 200
$response->parseJson();     // array

Also, you can use the setJson method to set a json string as the body.

use EasyHttp\SymfonyLayer\SymfonyClient;

$client = new SymfonyClient();

$client->prepareRequest('POST', 'https://jsonplaceholder.typicode.com/posts');
$client->getRequest()->setJson([
    'title' => 'foo',
    'body' => 'bar',
    'userId' => 1,
]);
$response = $client->execute();

$response->getStatusCode(); // 201
$response->parseJson();     // array

HTTP Authentication

Actually this library supports basic authentication natively.

use EasyHttp\SymfonyLayer\SymfonyClient;

$client = new SymfonyClient();

$client->prepareRequest('POST', 'https://api.sandbox.paypal.com/v1/oauth2/token');
$user = 'AeA1QIZXiflr1_-r0U2UbWTziOWX1GRQer5jkUq4ZfWT5qwb6qQRPq7jDtv57TL4POEEezGLdutcxnkJ';
$pass = 'ECYYrrSHdKfk_Q0EdvzdGkzj58a66kKaUQ5dZAEv4HvvtDId2_DpSuYDB088BZxGuMji7G4OFUnPog6p';
$client->getRequest()->setBasicAuth($user, $pass);
$client->getRequest()->setQuery(['grant_type' => 'client_credentials']);
$response = $client->execute();

$response->getStatusCode(); // 200
$response->parseJson();     // array