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\Controller\Snippets;

use App\Tests\WebTestCase;
use App\Utility\Ipstack;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
 * @covers Snippet99Trait::snippet99
 * @covers Ipstack::getPublicIp
 */
final class Snippet99Test extends WebTestCase
{
    private HttpClientInterface  $httpClient;

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

    /**
     * @covers Snippet99Trait::snippet99
     */
    public function testSnippet99(): void
    {
        $ip = $this->httpClient->request('GET', 'https://ipecho.net/plain', [
            'verify_peer' => false,
        ])->getContent();
        self::assertNotEmpty(filter_var($ip, FILTER_VALIDATE_IP));
    }
}

 More on Stackoverflow   Read the doc  Random snippet