Deactivate the SSL peer certificate check with the Symfony HTTP client

Published on 2021-03-20 • Modified on 2021-03-20

In this snippet, we will see how to deactivate the SSL peer certificate check with the Symfony HTTP client. When you trust the source and you want to avoid the following error: SSL peer certificate or SSH remote key was not OK, it can be useful (temporarily!). I use this in some of my tests like below. The trick is to pass the verify_peer option (3rd argument) with the false value.


<?php

declare(strict_types=1);

namespace App\Tests\External\Controller\Snippet;

use App\Tests\WebTestCase;
use App\Utility\AbstractApi;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
 * @see https://ipecho.net
 * @see Snippet99Trait::snippet99
 * @see AbstractApi::getPublicIp
 */
final class Snippet99Test extends WebTestCase
{
    private HttpClientInterface $httpClient;

    protected function setUp(): void
    {
        $this->httpClient = $this->getHttpClientService();
    }

    /**
     * @see Snippet99Trait::snippet99
     */
    public function testSnippet99(): void
    {
        try {
            $ip = $this->httpClient->request('GET', 'https://ipecho.net/plain', [
                'verify_peer' => false,
            ])->getContent();
        } catch (TransportException) {
            $ip = '127.0.0.1';
        }

        self::assertNotEmpty(filter_var($ip, FILTER_VALIDATE_IP));
    }
}

 More on Stackoverflow   Read the doc  Random snippet

  Work with me!