Windranger
Windranger

Reputation: 87

SoapClient php connect WCF services with x509 certificate

I'm using SoapClient in PHP to connect WS .NET. Here is a part of wsdl :

<wsa10:EndpointReference>
<wsa10:Address>-------------</wsa10:Address>
<Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
 <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
  <X509Data>
  <X509Certificate>
     hfWyLJxqZRtXrHw4slQBxEU8SGgHhQsYsRS...
  </X509Certificate>
  </X509Data>
 </KeyInfo>
</Identity>
</wsa10:EndpointReference>

Here is my code to connect :

$client = new SoapClient($wsdl,  array('soap_version' => SOAP_1_2, 'login' => 'login', 'password'=>'password' , 'trace' => 1));
$auth = new stdClass();
$auth->ecodedValue = $hash;
$header = new SoapHeader('http://schemas.xmlsoap.org/ws/2006/02/addressingidentity', 'identity', $auth, false);
$client->__setSoapHeaders($header);

and xml will be generated like that :

<?xml version="1.0" encoding="UTF-8"?> 
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
<env:Header>
<ns2:identity>
 <encodedValue>MIIEvjC</encodedValue>  
</ns2:identity>
</env:Header>
<env:Body>
<ns1:GetBalanceByPhoneNumber> 
<ns1:phoneNumber>------------</ns1:phoneNumber>
</ns1:GetBalanceByPhoneNumber>
</env:Body>
</env:Envelope>

What format of xml should I generate to pass certificate of WS? I'm waiting for your help :(.Thanks!

Upvotes: 1

Views: 2772

Answers (1)

lubosdz
lubosdz

Reputation: 4500

You can supply SSL certificate along with the soap request using OPTION "local_cert":

$client = new SoapClient($wsdl,  array(
   'local_cert' => '/path/to/your/certificate.pem',
   'soap_version' => SOAP_1_2, 
   'login' => 'login', 
   'password'=>'password' , 
   'trace' => 1,
));

Check out parameters for SOAP OPTIONS at http://php.net/manual/en/soapclient.soapclient.php.

You can also turn off SSL validation by creating stream context like so:

$context = stream_context_create(array(
    "ssl" => array(
        "verify_peer" => false,
        "allow_self_signed" => true,
    ),
    "https" => array(
        "curl_verify_ssl_peer" => false,
        "curl_verify_ssl_host" => false,
    ),
));


$options = array(
    "stream_context" => $context,
    "features" => SOAP_SINGLE_ELEMENT_ARRAYS,
    // "cache_wsdl" => WSDL_CACHE_NONE,
    // "trace" => true,
    // "exceptions" => true,
);

$client = new SoapClient("https://www.myhost.com/service?ws", $options);

Upvotes: 1

Related Questions