easy-http / symfony-layer
Http layer for Symfony Client
v0.3.0
2022-08-26 19:03 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- easy-http/layer-contracts: ^1.0
- symfony/http-client: ^5.1
Requires (Dev)
- guzzlehttp/guzzle: ^7.0
- phpunit/phpunit: ^9.2
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-10-27 18:50:12 UTC
README
Symfony Layer
This is an HTTP layer for Symfony Client. For more layers see Easy Http.
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