SoapClient php使用x509证书连接WCF服务


SoapClient php connect WCF services with x509 certificate

我在PHP中使用SoapClient来连接WS-NET。以下是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>

这是我要连接的代码:

$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);

xml将像这样生成:

<?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>

我应该生成什么格式的xml来通过WS证书?我在等你的帮助:(.谢谢!

您可以使用OPTION"local_cert"提供SSL证书和soap请求:

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

在中检查SOAP OPTIONS的参数http://php.net/manual/en/soapclient.soapclient.php.

您还可以通过创建如下流上下文来关闭SSL验证:

$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);