Add the required package using composer.
composer require freshcells/guzzle-bundle:^3.0composer require freshcells/guzzle-bundle:^2.0composer require freshcells/guzzle-bundle:@devAdd the bundle to your AppKernel.
// in %kernel.root_dir%/AppKernel.php
$bundles = array(
// ...
new Csa\Bundle\GuzzleBundle\CsaGuzzleBundle(),
// ...
);To enable the data collector (only in the dev environment, you may simply
configure the CsaGuzzleBundle as follows:
csa_guzzle:
profiler: '%kernel.debug%'You may also enable the included logger, in order log outcoming requests:
csa_guzzle:
logger: trueIf you rely on Symfony autowiring, you can choose to alias a specific service to the GuzzleHttp\ClientInterface
interface and GuzzlHttp\Client class.
csa_guzzle:
profiler: '%kernel.debug%'
logger: true
clients:
github_api:
config:
base_uri: 'https://api.github.com'
headers:
Accept: application/vnd.github.v3+json
jsonplaceholder:
config:
base_uri: 'https://jsonplaceholder.typicode.com'
headers:
Accept: application/json
default_client: github_apiThen, your github_api client will be automatically injected into your controller or service:
<?php
namespace App\Controller;
use Twig\Environment;
use Symfony\Component\HttpFoundation\Response;
use GuzzleHttp\Client;
class DefaultController
{
private $twig;
private $client;
public function __construct(Environment $twig, Client $client)
{
$this->twig = $twig;
$this->client = $client;
}
public function index()
{
$this->client->get('/users');
return new Response($this->twig->render("base.html.twig"), 200, ['Content-Type' => 'text/html']);
}
}Next section: Creating clients