2

I'm trying to find solution for my problem and I found this post. I implement it and get this error

cURL Error: SSL certificate problem: self signed certificate

This is what I did saving the cacert.pem, I copy and paste it to notepad and put this to my path C:\xampp\htdocs\test and when I save it, it turns to cacert.pem.txt. I don't know if this is the right way to do it. The post said need a PATHTO(which folder cacert.pem save, I think). I put this to php.ini

curl.cainfo = "C:\xampp\htdocs\test\cacert.pem.txt"

and this is the code of my curl.

function curl_generator($url,$postData = array()){

    if($url){
        $data_string = json_encode($postData);

        $ch = curl_init($url);
        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt ($ch, CURLOPT_CAINFO, 'C:\xampp\htdocs\test\cacert.pem.txt');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );

        $result = curl_exec($ch);
        if ($result === FALSE) {                
            echo "cURL Error: " . curl_error($ch);  
        }
        curl_close($ch);

        return json_decode($result);
    }else{
        return false;
    }
}

I don't know if this is the correct way. I'm now confuse and no idea of error.

Community
  • 1
  • 1
user3818576
  • 2,979
  • 8
  • 37
  • 62
  • Why do you need to install an SSL certificate? CURL is capable of reading a site's installed certificate – Machavity Nov 11 '15 at 01:22
  • @Machavity OP said the certificate is self-signed, so unless you somehow add a CA chain to a trusted root (even if that's your own root), the connection will fail. Correct me if I'm wrong. – Mike Nov 11 '15 at 01:25
  • because I can't find other way to connect. I try this curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); but no response I get – user3818576 Nov 11 '15 at 01:25
  • @user3818576 What do you mean by "no response"? What is the HTTP status code? – Mike Nov 11 '15 at 01:27
  • @Machavity I get this error if I used that code cURL Error: Failed connect to sample.com:443; No error – user3818576 Nov 11 '15 at 01:29
  • I assume it is a certificate error. so i try the code for certificate – user3818576 Nov 11 '15 at 01:30
  • @Mike Can't validate a self signed. But my question is why he needs to do this. You can connect to a site with SSL installed and CURL will negotiate like a browser – Machavity Nov 11 '15 at 01:42
  • @Machavity I dont have response now. I already fix the cURL Error: Failed connect to sample.com:443; No error . – user3818576 Nov 11 '15 at 01:53
  • @Mike saving cacert.pem in notepad. Is it a right way to do it? – user3818576 Nov 11 '15 at 03:25
  • @Machavity I get an error URL Error: error setting certificate verify locations: CAfile: g:\cacert.pem CApath: none – user3818576 Nov 11 '15 at 03:29
  • What are you trying to connect to? You're not providing us any context? – Machavity Nov 11 '15 at 13:09

1 Answers1

0

Replace this line

 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,1);

With

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
shalini
  • 1,291
  • 11
  • 13
  • or If you want to add certificate add in same folder root where are you running your script not in php.ini and provide path to that certificate – shalini Nov 13 '15 at 09:44