Search code examples
phpsslsoapcertificatephp-openssl

SOAP with two certificates file and "Could not connect to host"


I have an issue connecting PHP with an API which requires using SSL certificates. The issue is when I run my PHP code I get "Could not connect to host".

I was able to connect to the service provider using CURL like this :

$wsdl       = 'https://example.com/Case.svc';
$certFile   = getcwd() . '/certificate.crt';
$keyFile    = getcwd() . '/key.pem';
$password   = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,           $wsdl);
curl_setopt($ch, CURLOPT_SSLCERT,       $certFile);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD,  $password);
curl_setopt($ch, CURLOPT_SSLKEY,        $keyFile);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);

var_dump(curl_errno($ch));
var_dump(curl_error($ch));

However, I was not able to do this using SOAP:

$options=array(
            'soap_version'   => SOAP_1_2,
            'trace' => true,
            'cache_wsdl' => WSDL_CACHE_NONE,
            'exceptions' => 1,
            'encoding' => 'UTF-8',  
            'stream_context'=>stream_context_create(
                          array(
                            'ssl'=>array(
                                'verify_peer'=>true
                                ,'allow_self_signed'=>false
                                ,'cafile'=>'certificate.crt'
                                ,'verify_depth'=>5
                                )
                            )
                        ), 
                        'local_cert' => 'key.pem',
            'passphrase' => 'password',     
        );

    $client = new SoapClient("Case.wsdl", $options);

It results in "Could not connect to host".

The Issue might be in the certificate itself, maybe I need to merge two certificate in one file. I can see that many people having the same issue, most of them did not get an answer.

Why is the SOAP code returning "Could not connect to host", and how do I fix it?


Solution

  • Some places for you to start:

    1) What exactly is certificate.crt? Is it a client certificate, or a CA certificate? You are using CURLOPT_SSLCERT (sets the client certificate), but are using the 'cafile' file directive in SoapClient (sets the CA cert file).

    2) You are using CURLOPT_SSL_VERIFYPEER = false, but in the other snippet you use verify_peer.

    So try debugging a bit more with these changes, having the same type of environment.