
As it is known the csa guzzle package is no longer maintained. Still many projects use it, and therefore will have to switch client in the short term.

One of the projects I’m working on was using Csa Guzzle and in a very short time I switched to Symfony HttpClient.
Obviously the first thing to do is to install the package:
$ composer require symfony/http-client
Then some modification in the config files is required.
The csa configuration file named csa_guzzle.yaml, which is used to declare clients, was this:
#csa_guzzle.yaml
csa_guzzle:
profiler: "%kernel.debug%"
clients:
api:
config:
base_uri: "%api_uri%"
headers:
Accept: application/json
Host: "%api_host%"
User-Agent: "%api_user_agent%"
With HttpClient we will have to declare the clients in the framework.yaml file under the scoped_clients option:
#framework.yaml
framework:
...
http_client:
default_options:
headers:
Accept: application/json
Content-Type: application/json
User-Agent: "%api_user_agent%"
scoped_clients:
http.client.api:
base_uri: '%api_uri%'
headers:
Host: "%api_host%"
Then remove the csa_guzzle.yaml file

and also the Guzzle Http Client declaration on services.yaml file (if it is defined):

Don’t forget to remove the bundle from the bundle system configuration (config/bundles.php):

Now I just need to inject the new client into my app client class.
Note that my interface remains unchanged:
interface AppClientInterface
{
public function request(string $method, string $path, array $options = []): string;
}
But my class now uses the new client:


And a small change also in the method making the request, to retrieve properly the content of the response:


Here my complete client class:
<?php
namespace App\Client;
use App\Client\Exception\ApiException;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class Client implements AppClientInterface
{
/** @var HttpClientInterface */
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
public function request(string $method, string $path, array $options = []): string
{
try {
$response = $this->client->request($method, $path, $options);
$content = $response->getContent();
} catch (ClientException $e)
throw ApiException::fromClientException($e);
}
return $content;
}
}
Finally, you just have to uninstall csa via composer:
$ composer remove csa/guzzle-bundle
Comments