YII2 client->createRequest() ignore verification

Viewed 281

I'm trying to call an external API from my Yii Application

$client = new Client();
$response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl('https//externalApi')
    ->setData(['name' => 'John Doe', 'email' => 'johndoe@example.com'])
    ->send();
if ($response->isOk) {
    var_dump($response);
}

But I get:

HTTP Client Exception: fopen(): Peer certificate CN=`****' did not match expected CN=`****'

I think it's something related to the SSL certificate. Is there a way to ignore the verification ?

1 Answers

I found the solution, you need to set the options to disable SSL checking with a ssl prefix, so do the request like this:

$client = new Client();
$response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl('https//externalApi')
    ->setData(['name' => 'John Doe', 'email' => 'johndoe@example.com'])
    ->setOptions([
       'sslallow_self_signed' => true,
       'sslverify_peer_name'     => false,
      ])
    ->send();
if ($response->isOk) {
    var_dump($response);
}
Related