cURL error 60: SSL certificate in Laravel 5.4

2019-01-23 18:10发布

问题:

Full Error

RequestException in CurlFactory.php line 187: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

Scenario

Before anyone points me to these two laracasts answers: https://laracasts.com/discuss/channels/general-discussion/curl-error-60-ssl-certificate-problem-unable-to-get-local-issuer-certificate

https://laracasts.com/discuss/channels/general-discussion/curl-error-60-ssl-certificate-problem-unable-to-get-local-issuer-certificate/replies/52954

I've already looked at them and thats why im here,

The problem i have is that i now have the cacert.pem file BUT it doesn't make sense where to put it, the answers indicate to place the file in my xampp directory and change my php.ini file but im not using xampp for anything, im using laravel's artisan server to run my project. If xampp is not in use then where do i place this file & more so why would an accepted answer be to place it in my xampp directory i dont understand

My Exact Question

Where do i place the cacert.pem file to stop this error in laravel 5.4?

回答1:

Do not ever modify files in the vendor/ folder. Ever. They can and will be overwritten on the next composer update you run.

Here is my Solution for WampServer

I am using PHP 7.1.9 for my WampServer, so change 7.1.9 in the example below to the version number you are currently using.

  1. Download this file: http://curl.haxx.se/ca/cacert.pem
  2. Place this file in the C:\wamp64\bin\php\php7.1.9 folder
  3. Open php.iniand find this line:

;curl.cainfo

Change it to:

curl.cainfo = "C:\wamp64\bin\php\php7.1.9\cacert.pem"

Make sure you remove the semicolon at the beginning of the line.

Save changes to php.ini, restart WampServer, and you're good to go!



回答2:

This was stressfull to figure out but here is the exact answer for people using laravel and have this problem.

My exact application versions are...

Laravel: 5.4

Guzzlehttp: 6.2

Laravel Socialite: 3.0

Download a fresh copy of this curl certificate from this link: https://gist.github.com/VersatilityWerks/5719158/download

Save the file in this path starting from the base root of your laravel application vendor/guzzlehttp/guzzle/src/cacert.pem

next in that same directory open up RequestOptions.php and scroll down to the constant called CERT and change it to this const CERT = 'cacert.pem'; and this should fix it all.

EDIT

As people are pointing out you should never edit the vendor folder, this was just a quick fix for an application I was building in my spare time. It wasn't anything majorly important like an application for my company or anything, use this method at your own risk! Please check out answers below



回答3:

A quick solution but insecure (not recommended).

Using cURL:

Set CURLOPT_SSL_VERIYPEER to false

Using Guzzle:

Set verify to false

example $client->request('GET', 'https://somewebsite.com', ['verify' => false]);



回答4:

Another one recently asked for the same problem and it's seems my answer was the solution for him. Here was the post I mention : URL Post

That's what I said :

I'll be fully honest, I don't know anything about Laravel. But I had the same problem, so as many other, on Symfony. And so as you I tried many things without success.

Finally, this solution worked for me : URL solution

It indicates that instead of a certificate problem, it could came from a environnement non-compatibility. I used XAMPP instead of WAMP and it worked.



回答5:

Solution suggested by some users to make changes to \vendor\guzzlehttp\guzzle\src\Client.php file is the worst advice, as manual changes made to vendor folder are overwritten if you run composer update command.


Solution suggested by Jeffrey is a dirty, shorthand fix but not recommended in production applications.


Solution suggested by kjdion84 is perfect if you have access to php.ini file on web server. In case you are using Shared Hosting, it may not be possible to edit php.ini file.


When you don't have access to php.ini file (e.g. Shared Hosting)

  1. Download this file: http://curl.haxx.se/ca/cacert.pem
  2. Place this file in the root folder of your Laravel project.
  3. Add verify key to GuzzleHttp\Client constructor with its value as path to cacert.pem file.

With Laravel 5.7 and GuzzleHttp 6.0

// https://example.com/v1/current.json?key1=value1&key2=value2

$guzzleClient = new GuzzleHttp\Client([
    'base_uri' => 'https://example.com',
    'verify' => base_path('cacert.pem'),
]);

$response = $guzzleClient->get('v1/current.json', [
    'query' => [
        'key1' => 'value1',
        'key2' => 'value2',
    ]
]);

$response = json_decode($response->getBody()->getContents(), true);


回答6:

for Laravel: The 5 steps below will be helpful

  • update version to Guzzlehttp: 5.2
  • find the file under \vendor\guzzlehttp\guzzle\src\Client.php
  • edit default settings to

    protected function getDefaultOptions() { $settings = [ 'allow_redirects' => true, 'exceptions' => true, 'decode_content' => true, 'verify' => getcwd() .'/vendor/guzzlehttp/guzzle/src/cacert.pem' ]; }

  • download latest file cacert.pem from http://curl.haxx.se/ca/cacert.pem and place under /vendor/guzzlehttp/guzzle/src/