Laravel & PHP use Goutte not to verify SSL certificate

Foreword:

Goutte is a PHP library for scraping website data. It provides an elegant API, which makes it easy to select specific elements from a remote page.

1. Error analysis:

When requesting a https website, the following error occurs:

​ SSL peer certificate or SSH remote key was not OK

The literal translation is: the SSL peer certificate or SSH remote key is abnormal. In fact, it is necessary to set curl to not verify certificates and hosts. The settings in curl are as follows:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

 But calling Goutte in Laravel or PHP is a new class directly, and curl cannot be set directly.

use Goutte\Client

$client = new Client();
$client->request('GET', $url);

2. Problem solving

By going to the official github to view, there is the following description

If you need to use your own HTTP settings, you can create an instance of HttpClient and pass it to Goutte. For example, to add a request timeout of 60 seconds:

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;

$client = new Client(HttpClient::create([
    'timeout' => 60
]));

How about setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST? The parameters are as follows

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;

$client = new Client(HttpClient::create([
    'timeout' => 60
    'verify_peer' => false,
    'verify_host' => false,
]));

If you need to see other HTTP settings, please see the symfony documentation:

Framework Configuration Reference (FrameworkBundle) (Symfony Docs)https://symfony.com/doc/current/reference/configuration/framework.html#reference-http-client

 

Guess you like

Origin blog.csdn.net/qq_27295403/article/details/127259798