Search code examples
phpsslsoapwsdlcodeigniter-2

Soap service with an ssl certificate with password (PHP)


I need to access a SOAP service with a certificate protected by password. I'm new in PHP (with PHP 5.4 in CodeIgniter 2) and have tried some options that doesn't work for me.

I have the following constants:

const WSDL  = 'https://sedeapl.dgt.gob.es:8080/WS_IEST_COMP/descargaArchivoMicrodatosService?wsdl';

const XMLNS = 'https://sedeapl.dgt.gob.es:8080/WS_IEST_COMP/descargaArchivoMicrodatosService';

const LOCAL_CERT_PASSWD = 'HERE I HAVE THE PASS OF THE CERT';
const LOCAL_CERT = './certificados/Certificados.p12';

private $client;

I have tried these options:

Option A

$this->client = new SoapClient(self::WSDL, array(
                "trace"         => 1, 
                "exceptions"    => true, 
                "local_cert"    => self::LOCAL_CERT, 
                "uri"           => "urn:xmethods-delayed-quotes",
                "style"         => SOAP_RPC,
                "use"           => SOAP_ENCODED,
                "soap_version"  => SOAP_1_2 ,
                "location"      => self::XMLNS
            )
        );

Options B

$this->$client = new SoapClient(self::WSDL, array('local_cert' => self::LOCAL_CERT));

I have no idea how to add the password. Those solutions are what I found here on StackOverflow. In both examples I get the same error:

SoapClient::SoapClient(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

I have uncomented the "extension=php_openssl.dll" in php.ini

I've tried with these routes of cert:

const LOCAL_CERT = 'certificados/Certificados.p12';
const LOCAL_CERT = 'Certificados.p12';
const LOCAL_CERT = './certificados/Certificados.p12';

Does anyone have an idea about what can I do. Thank you very much!


Solution

  • first of all you need to convert .p12 to .pem
    in Linux you can do it with following command
    openssl pkcs12 -in Certificados.p12 -out Certificados.pem -clcerts

    then try the following:

    $options = array();
    
    $options['trace'] = true;
    $options['exceptions'] = true;
    $options['local_cert'] = 'path to your .pem file';
    $options['passphrase'] = self::LOCAL_CERT_PASSWD;
    
    try {
        $soapClient = new SoapClient(self::WSDL, $options);
    
        echo '<pre>';        
        // wsdl methods
        print_r($soapClient->__getFunctions());        
        echo '</pre>';
    }
    catch (Exception $e)
    {
        echo $e->getMessage(), '<br />', $e->getTraceAsString();
    }