Solution to SSL certificate problem: unable to get local issuer certificate in php

When you need to access https locally using curl or some other encapsulated http class library or component (such as Guzzle, a well-known http client in the PHP industry), if there is no local certificate configured, an SSL certificate problem will occur: unable to get local issuer certificate error message.
Solution 1 (environment configuration)
① Download the pem file

②. Copy the file to any directory. Here I copied it to the D:/Visual-NMP-x64/Bin/PHP/php-7.4.13-nts-x64 directory.

③In the php.ini configuration file, configure the value of the curl.cainfo configuration item. For example, my configuration here is: curl.cainfo = “D:/Visual-NMP-x64/Bin/PHP/php-7.4.13-nts-x64 /cacert.pem”

④. Finally, restart the environment.
Solution 2 (code level)
① If you are using curl-related functions, you can make the following settings:

If you directly use PHP's built-in curl-related functions, you can add the following code to the curl-related code to indicate that SSL is not checked (this method is generally used for local debugging.)

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //停止验证证书(一般只需要设置此项为false即可)
 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)。译者注:公用名(Common Name)一般来讲就是填写你将要申请SSL证书的域名 (domain)或子域名(sub domain)。 设置成 2,会检查公用名是否存在,并且是否与提供的主机名匹配。 在生产环境中,这个值应该是 2(默认值)。这里我们也可以直接设置为false
 
 
//如果不是有什么特别细节的问题的话,我们一般只需要设置上面哪一行代码 即 CURLOPT_SSL_VERIFYPEER选项 为false 即可。

②. If you are using the Guzzle class library, the code can be set as follows:

$client = new GuzzleHttp\Client();
 
//以下是让Guzzle取消ssl证书验证!!!
$client->request('GET', '/', ['verify' => false]); //取消ssl验证
 
 
 
 
//以下是让Guzzle使用ssl证书验证!!!别看错了!!!!!!
// 使用系统的CA包 (默认设置)
$client->request('GET', '/', ['verify' => true]); //这个可以不设置,因为默认就是true
 
//使用磁盘上自定义的SSL证书
$client->request('GET', '/', ['verify' => '/path/to/cert.pem']);
 
 
//注意:Guzzle以上代码只在Guzzle6的版本中使用过,如果是其它低版本或者Guzzle升级了版本,写法可能有变动,记得查阅官方文档即可

Guess you like

Origin blog.csdn.net/qq_24023151/article/details/132779085