Error using PHP cURL with SSL certificates

前端 未结 5 1801
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 10:03

I\'m trying to write a PHP script using cURL that can authorize a user through a page that uses an SSL certificate, in addition to username and password, and I can\'t seem t

5条回答
  •  情书的邮戳
    2020-12-08 10:54

    To elaborate and sum this up:

    if you have a PHP file using PHP curl and place the ca certificate for your systems in the same directory, the below code will give you a jumpstart

        $url = "https://myserver.mydomain.local/get_somedata.php";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
    
    //These next lines are for the magic "good cert confirmation"
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
    
    //for local domains:
    //you need to get the pem cert file for the root ca or intermediate CA that you signed all the domain certificates with so that PHP curl can use it...sorry batteries not included
    //place the pem or crt ca certificate file in the same directory as the php file for this code to work
        curl_setopt($ch, CURLOPT_CAINFO, __DIR__.'/cafile.pem');
        curl_setopt($ch, CURLOPT_CAPATH, __DIR__.'/cafile.pem');
    
    //DEBUG: remove slashes on the next line to prove "SSL verify" is the cause       
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    //Error handling and return result
        $data = curl_exec($ch);
        if ($data === false) {
            $result = curl_error($ch);
        } else {
            $result = $data;
        }
    
    // Close handle
        curl_close($ch);
        return $result;
    

提交回复
热议问题