0

I have problem with curl requests on El Capitan 10.11.4, PHP 7.0.12, CURL 7.50.3. When I try to make requests to https services then I see error: Curl failed with error #51: SSL: certificate verification failed (result: 5)

Do you have idea how can I fix it?

try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://google.ch');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);

    if (FALSE === $content)
        throw new Exception(curl_error($ch), curl_errno($ch));

} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}
Hubert Burdach
  • 91
  • 2
  • 15
  • You could add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); as a quickfix. A better way is described here http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ – Bolli Dec 02 '16 at 08:08
  • but now (with `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)`) I see: Curl failed with error #35: SSL: CA certificate set, but certificate verification is disabled – Hubert Burdach Dec 02 '16 at 08:18
  • You might also need curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); There is more info about this here: http://stackoverflow.com/questions/6400300/https-and-ssl3-get-server-certificatecertificate-verify-failed-ca-is-ok – Bolli Dec 02 '16 at 08:22
  • Now I have: `$ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, 'https://google.ch'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch);` and still I have the same error... – Hubert Burdach Dec 02 '16 at 08:28
  • Did you try the other and better solution by exporting the certificate and set cacert.pem to curl.cainfo ? As described in both links? – Bolli Dec 02 '16 at 08:31
  • I see that I can add certificate: `curl_setopt($ch, CURLOPT_CAINFO, 'path/to/cert');` but how can I generate cert file? – Hubert Burdach Dec 02 '16 at 10:57
  • This file doesn't work :/ https://curl.haxx.se/ca/cacert.pem. I see error... – Hubert Burdach Dec 02 '16 at 11:06
  • You need to grab the certificate from the url you want to use curl on. Did you do that? See my first link. – Bolli Dec 02 '16 at 11:10
  • 1
    Yes, I downloaded cert file from https://pki.google.com/ and it works :) Thank you Bolli! – Hubert Burdach Dec 02 '16 at 11:28

0 Answers0