使用具有安全模式的SoapClient用PHP创建SOAP头“;TransportWithMessageCredenti


Creating a SOAP header with PHP using SoapClient with security mode "TransportWithMessageCredential“

如何使用SoapClient在PHP中使用"TransportWithMessageCredential"模式构建SOAP标头。我正在使用SoapClient,因为这似乎是最好的解决方案。以下内容来自给定的文档:

Web服务使用安全模式"TransportWithMessageCredential"。为了安全传输,正在使用SSL证书。此外,为了消息交换的安全性,需要用户名和密码的组合。用户名和密码在SOAP标头中传输。

示例:

<soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasisopen.
    org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss wssecurityutility-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-37">
            <wsse:Username>MyUsername</wsse:Username>
            <wsse:Password Type="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword!</wsse:Password>
        <wsse:Nonce EncodingType="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">vAvnhyzl+yP8Yb8ZVdKnMw==</wsse:Nonce>
        <wsu:Created>2014-03-17T13:08:02.795Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>  

其中"MyUserName"answers"MyPassword!"为c。与实际登录信息交换。

wsdl可用于提供的每个功能。

此类返回具有以上定义格式的头:)

class WsseAuthHeader extends SoapHeader
{
    private $wssNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    private $wsuNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
    private $passType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    private $nonceType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
    private $username = 'Username';
    private $password = 'Password';

    function __construct()
    {
        $created = gmdate('Y-m-d'TH:i:s'Z');
        $nonce = mt_rand();
        $encodedNonce = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*', $created) . pack('a*', $this->password))));
        // Creating WSS identification header using SimpleXML
        $root = new SimpleXMLElement('<root/>');
        $security = $root->addChild('wsse:Security', null, $this->wssNs);
        $usernameToken = $security->addChild('wsse:UsernameToken', null, $this->wssNs);
        $usernameToken->addChild('wsse:Username', $this->username, $this->wssNs);
        $passNode = $usernameToken->addChild('wsse:Password', htmlspecialchars($this->password, ENT_XML1, 'UTF-8'), $this->wssNs);
        $passNode->addAttribute('Type', $this->passType);
        $nonceNode = $usernameToken->addChild('wsse:Nonce', $encodedNonce, $this->wssNs);
        $nonceNode->addAttribute('EncodingType', $this->nonceType);
        $usernameToken->addChild('wsu:Created', $created, $this->wsuNs);
        // Recovering XML value from that object
        $root->registerXPathNamespace('wsse', $this->wssNs);
        $full = $root->xpath('/root/wsse:Security');
        $auth = $full[0]->asXML();
        parent::SoapHeader($this->wssNs, 'Security', new SoapVar($auth, XSD_ANYXML), true);
    }
};