我该如何创建像这样的soap标头?

时间:2022-08-22 20:21:22

Doing some SOAP calls to a 3rd party application. They provide this soap header as an example of what the application expects. How can I create a SOAP header like this in PHP?

对第三方应用程序执行一些SOAP调用。它们提供此soap标头作为应用程序所期望的示例。如何在PHP中创建这样的SOAP标头?

<SOAP-ENV:Header>
    <NS1:Security xsi:type="NS2:Security" xmlns:NS1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:NS2="urn:dkWSValueObjects">
        <NS2:UsernameToken xsi:type="NS2:TUsernameToken">
            <Username xsi:type="xsd:string">XXXX</Username> 
            <Password xsi:type="xsd:string">XXX</Password> 
        </NS2:UsernameToken>
    </NS1:Security>
</SOAP-ENV:Header>

I do what i think is a correct call and keep getting in return that no headers were sent.

我做我认为是正确的电话,并继续得到没有发送标题。

Here is a sample from my code.

这是我的代码中的一个示例。

class SOAPStruct 
{
    function __construct($user, $pass) 
    {
        $this->Username = $user;
        $this->Password = $pass;
    }
}

$client = new SoapClient("http://www.example.com/service");

$auth = new SOAPStruct("username", "password");
$header = new SoapHeader("http://example.com/service", "TUsernameToken", $auth);

$client->__setSoapHeaders(array($header));
$client->__soapCall("GetSubscriptionGroupTypes", array(), NULL, $header)

And this is the SOAP header i get back. (its more but i stripped info away that might be sensitive)

这是我回来的SOAP标题。 (它更多,但我剥离了可能敏感的信息)

<SOAP-ENV:Header>
    <ns2:TUsernameToken>
        <Username>username</Username> 
        <Password>password</Password> 
    </ns2:TUsernameToken>
</SOAP-ENV:Header>

1 个解决方案

#1


SOAP header handling in PHP is actually not very flexible and I'd go as far as saying that especially the use two namespaces within the header will make it impossible to inject the header simply by using a SoapHeader-construct of some type.

PHP中的SOAP标头处理实际上并不是非常灵活,我甚至会说,特别是在标头中使用两个名称空间将使得仅通过使用某种类型的SoapHeader构造来注入标头是不可能的。

I think the best way to handle this one is to shape the XML request yourself by overriding SoapClient::__doRequest() in a custom class that extends SoapClient.

我认为处理这个问题的最佳方法是通过在扩展SoapClient的自定义类中重写SoapClient :: __ doRequest()来自行调整XML请求。

class My_SoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $xmlRequest = new DOMDocument('1.0');
        $xmlRequest->loadXML($request);

        /*
         * Do your processing using DOM 
         * e.g. insert security header and so on
         */

        $request = $xmlRequest->saveXML();
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}

Please see SoapClient::__doRequest for further information and some examples.

有关更多信息和一些示例,请参阅SoapClient :: __ doRequest。

#1


SOAP header handling in PHP is actually not very flexible and I'd go as far as saying that especially the use two namespaces within the header will make it impossible to inject the header simply by using a SoapHeader-construct of some type.

PHP中的SOAP标头处理实际上并不是非常灵活,我甚至会说,特别是在标头中使用两个名称空间将使得仅通过使用某种类型的SoapHeader构造来注入标头是不可能的。

I think the best way to handle this one is to shape the XML request yourself by overriding SoapClient::__doRequest() in a custom class that extends SoapClient.

我认为处理这个问题的最佳方法是通过在扩展SoapClient的自定义类中重写SoapClient :: __ doRequest()来自行调整XML请求。

class My_SoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $xmlRequest = new DOMDocument('1.0');
        $xmlRequest->loadXML($request);

        /*
         * Do your processing using DOM 
         * e.g. insert security header and so on
         */

        $request = $xmlRequest->saveXML();
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}

Please see SoapClient::__doRequest for further information and some examples.

有关更多信息和一些示例,请参阅SoapClient :: __ doRequest。