Skip to content

easy-http/symfony-layer

Repository files navigation

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