4

我的 SOAP 请求的标头以一种奇怪的格式显示。我需要一个看起来像这样的标题:

<soap-env:header>
    <wsse:security xmlns:wsse="http://docs.oasis-open.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-wssecurity-utility-1.0.xsd">
        <wsse:usernametoken wsu:id="UsernameToken-45">
            <wsse:username>817221</wsse:username>
            <wsse:password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:password>
        </wsse:usernametoken>
    </wsse:security>
    </soap-env:header>

现在,标题看起来像这样:

<SOAP-ENV:Header>
    <ns8:Security SOAP-ENV:mustUnderstand="1">
        <item>
            <key>UsernameToken</key>
            <value>
                <item>
                    <key>Username</key>
                    <value>817221</value>
                </item>
                <item>
                    <key>Password</key>
                    <value>
                        <item>
                            <key>_</key>
                            <value>1234</value>
                        </item>
                        <item>
                            <key>Type</key>
                            <value>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText</value>
                        </item>
                    </value>
                </item>
            </value>
        </item>
    </ns8:Security>
</SOAP-ENV:Header>

大错特错。它包含和标签。我读过应该使用 SOAP_ENC_OBJECT 以正确的格式显示它,所以我在我的代码中尝试了它,但仍然无法正常工作。请看下面的代码:

$header = array(
            'UsernameToken' => array(
                'Username' => 817221,
                'Password' => array(
                        '_' => 1234,
                        'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')));

$headerSoapVar = new SoapVar($header,SOAP_ENC_OBJECT); 
$soapheader = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', "Security" , $header, true);
$client->__setSoapHeaders($soapheader);

任何帮助将不胜感激。谢谢!

4

2 回答 2

4

这是一个很老的问题!

但是,我今天遇到了同样的问题!

我发现“标题”需要是一个对象,而不是一个数组!

但是我仍然遇到命名空间问题......所以我解决了 Sub-classing SoapClient 类

class MySoapClient extends SoapClient {

    public function __doRequest($request, $location, $action, $version, $one_way=0) {
        // manipulate $request var using XML parse tools or whatever !!
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}

我仍在解决这个问题,但我希望它可以帮助某人!

于 2013-11-18T16:19:58.983 回答
2

试试这个,你应该将它设置为一个对象。

$header = (object) array(
            'UsernameToken' => array(
                'Username' => 817221,
                'Password' => array(
                        '_' => 1234,
                        'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')));
于 2014-12-02T11:27:29.940 回答