构建SOAP标头

时间:2016-07-18 14:46:37

标签: php xml web-services soap

我需要为项目使用SOAP Web服务。

Web服务需要通过SOAP标头进行身份验证,如下所示:

<soap:Header>
    <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsse:UsernameToken>
            <wsse:Username>STAGING_WS</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">ANDREANI</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soap:Header>

所以,我尝试使用SoapHeader类构建头文件如下:

$ns = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

$auth = new \StdClass();
$auth->Username = new \SoapVar('STAGING_WS', XSD_STRING, NULL, $ns, NULL, $ns);
$auth->Password = new \SoapVar('ANDREANI', XSD_STRING, NULL, $ns, NULL, $ns);

$token = new \StdClass();
$token->UsernameToken = new \SoapVar($auth, SOAP_ENC_OBJECT, NULL, $ns, 'UsernameToken', $ns);
$security = new \SoapVar(new \SoapVar($token, SOAP_ENC_OBJECT, NULL, $ns, 'UsernameToken', $ns), SOAP_ENC_OBJECT, NULL, $ns, 'Security', $ns);

$header = new \SoapHeader('wsse', 'Security', $security, true);

但是,generatd标题并不相同:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.andreani.com.ar" xmlns:ns2="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns3="wsse">
<env:Header>
<ns3:Security env:mustUnderstand="true">
<ns2:UsernameToken>
<ns2:Username>STAGING_WS</ns2:Username>
<ns2:Password>ANDREANI</ns2:Password>
</ns2:UsernameToken>
</ns3:Security>
</env:Header>
<env:Body>
<ns1:ConfirmarCompra/>
</env:Body>
</env:Envelope>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试将最后一行更改为:

$header = new \SoapHeader($ns, 'Security', $security, true);

这会使Security标记的命名空间与UsernameTokenUsernamePassword标记相同。安全命名空间的本地名称仍然是ns2而不是wsse,但这并不重要。只要命名空间URL正确,正确的SOAP服务器就必须接受它。