SSL certificate error in cURL

2019-05-22 16:26发布

Facts

I have Apache/2.4.27 (Win64) PHP/7.2.0beta3 on Win 10 laptop. I want to implement cURL. This is my code

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, 'https://www.youtube.com');
curl_exec($curl);

if (curl_errno ( $curl )) {
    echo curl_error ( $curl );
    curl_close ( $curl );
}

that generates the error SSL certificate problem: unable to get local issuer certificate. So I downloaded certificates from https://curl.haxx.se/ca/cacert.pem. Took the cacert.pem file, put it in the PHP folder and edited the php.ini file like this curl.cainfo = C:php/ext/cacert.pem.

Problem

Now I get this error error setting certificate verify locations: CAfile: C:php/ext/cacert.pem CApath: none. I googled and the only solution I found is that I have to download the .crt file from the site I want to cURL and include it in my cURL like curl_setopt($curl, CURLOPT_CAINFO, "C:/wamp64/www/GIAG2.crt");.

Problem 1.1 : I dont know how to download the .crt file so I can include it in my code, like the above example.

Problem 1.2 : That is not a "universal" solution, I want to set my certificates in such way that I dont have to download different certificates for different site.

Thank you

标签: php ssl curl https
2条回答
孤傲高冷的网名
2楼-- · 2019-05-22 16:42

The easiest way is to skip this check.

...
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($curl);
查看更多
戒情不戒烟
3楼-- · 2019-05-22 16:52

The current CA Cert extracts provided by cURL contain the GeoTrust Global CA certificate authority which signed Google's CA cert which in turn signs YouTube's cert, so you should have no problem using the file you have.

Based on the last error, it looks like the problem is because you were missing the / after C:. The message error setting certificate verify locations means that it couldn't open or read the file specified by curl.cainfo so it's not finding any certs at all.

If you change C:php/ext/cacert.pem to C:/php/ext/cacert.pem it should be able to read the CA file correctly and then verify the site properly.

查看更多
登录 后发表回答