PHP SoapClient的复杂标题

时间:2017-06-23 16:55:32

标签: php xml soap request soap-client

我需要将以下复杂标头添加到我的SoapClient标头中。

<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsse:Security soapenv:mustUnderstand="1" 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>
            <wsse:Username>****</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*****</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
    <wsa:Action>/IntS5/S5WS</wsa:Action>
</soapenv:Header>

正如在其他答案中提出的,我在我的php项目中尝试了以下方法。因为我需要WSA寻址,所以我将wsa:Action与安全头一起设置。

    $header_part = '<wsse:Security soapenv:mustUnderstand="1" 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><wsse:Username>****</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">****</wsse:Password>'
         . '</wsse:UsernameToken></wsse:Security><wsa:Action>/IntS5/S5WS</wsa:Action>';

    $soap_var_header = new SoapVar($header_part, XSD_ANYXML, null, null, null);
    $soap_header = new SOAPHeader('http://www.w3.org/2005/08/addressing', 'wsa', $soap_var_header);
    $client->__setSoapHeaders($soap_header);

__soapCall如何返回以下错误。

SoapFault异常:SOAP不支持[客户端] DTD。我不确定它是否与标头或putCall的参数有关。有人可以帮帮我吗?

编辑:问题可能与HEADER / Envelope的命名空间有关。

发送到服务器的请求如下所示。 已更新

<?xml version="1.0" encoding="UTF-8"?>
                    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.s5.mediasat.de/" xmlns:ns2="http://www.w3.org/2005/08/addressing">
                        <env:Header>
                            <wsse:Security env:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                                <wsse:UsernameToken>
                                    <wsse:Username>****</wsse:Username>
                                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*****</wsse:Password>
                                </wsse:UsernameToken>
                            </wsse:Security>
                            <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">/IntS5/S5WS</wsa:Action>
                        </env:Header>
                        <env:Body>
                            <ns1:putCall>
                                <transaction>createIncident</transaction>
                                <transactionSender>Request</transactionSender>
                                <caseDataModel>
                                    <senderId>7</senderId>
                                    <ticketTypeId>102</ticketTypeId>
                                    <title>test</title>
                                    <priorityId>101</priorityId>
                                    <categoryId>128</categoryId>
                                    <description>Description</description>
                                    <ticketNumberSender>INCC00000743809</ticketNumberSender>
                                    <createTypeId>701</createTypeId>
                                    <serviceId>B001APP05K</serviceId>
                                    <categoryId>128</categoryId>
                                    <serviceRecipientId>77888</serviceRecipientId>
                                    <serviceLocationSAPCode>V135</serviceLocationSAPCode>
                                </caseDataModel>
                            </ns1:putCall>
                        </env:Body>
                    </env:Envelope>

适用于SOAP UI的请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.s5.mediasat.de/">
    <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
        <wsse:Security soapenv:mustUnderstand="1" 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>
                <wsse:Username>***</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">***</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
        <wsa:Action>/IntS5/S5WS</wsa:Action>
    </soapenv:Header>
    <soapenv:Body>
        <ws:putCall>
            <transaction>createIncident</transaction>
            <transactionSender>Request</transactionSender>
            <caseDataModel>
                <senderId>7</senderId>
                <ticketTypeId>102</ticketTypeId>
                <title>test</title>
                <priorityId>101</priorityId>
                <categoryId>128</categoryId>
                <description>Description</description>
                <ticketNumberSender>INCC00000743809</ticketNumberSender>
                <createTypeId>701</createTypeId>
                <serviceId>B001APP05K</serviceId>
                <categoryId>128</categoryId>
                <serviceRecipientId>77888</serviceRecipientId>
                <serviceLocationSAPCode>V135</serviceLocationSAPCode>
            </caseDataModel>
        </ws:putCall>
    </soapenv:Body>
</soapenv:Envelope>

无论如何设置/覆盖标题的命名空间?

2 个答案:

答案 0 :(得分:3)

您应该在程序上生成标头而不是使用字符串。

示例:

<?php

$client = new SoapClient(null, array(
    'location' => "http://localhost/soap/",
    'uri' => "http://ws.s5.mediasat.de/",
    'soap_version'   => SOAP_1_2
));

$username = '***';
$password = '***';
$sec_namespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$wsa_namespace = 'http://www.w3.org/2005/08/addressing';

// prepare security token
$node1 = new \SoapVar($username, XSD_STRING, null, null, 'Username', $sec_namespace);

$xml = new XMLWriter(); // this is a little hacky, but no other way to set the type as far as I know
$xml->openMemory();
$xml->startElementNS('wsse', 'Password',$sec_namespace);
$xml->writeAttribute('Type', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText');
$xml->Text($password);
$xml->endElement();
$node2 = new \SoapVar($xml->outputMemory(), XSD_ANYXML);

$token = new \SoapVar(array($node1, $node2), SOAP_ENC_OBJECT, null, null, 'UsernameToken', $sec_namespace);
$security = new \SoapVar(array($token), SOAP_ENC_OBJECT, null, null, 'Security', $sec_namespace);
$soap_header_sec = new \SOAPHeader($sec_namespace, 'Security', $security, true);

// prepare action token
$action = new \SoapVar('/IntS5/S5WS', XSD_STRING, null, null, 'Action', $wsa_namespace);
$soap_header_action = new \SOAPHeader($wsa_namespace, 'Action', $action, false);

// set prepared headers
$client->__setSoapHeaders(
    [$soap_header_sec,$soap_header_action]
 );

$client->putCall();

这将生成包含所有命名空间的有效信封:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.s5.mediasat.de/" xmlns:ns2="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns3="http://www.w3.org/2005/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
  <env:Header>
    <ns2:Security env:mustUnderstand="true">
      <ns2:UsernameToken>
        <ns2:Username>***</ns2:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">***</wsse:Password>
      </ns2:UsernameToken>
    </ns2:Security>
    <ns3:Action>/IntS5/S5WS</ns3:Action>
  </env:Header>
  <env:Body>
    <ns1:putCall env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" />
  </env:Body>
</env:Envelope>

答案 1 :(得分:0)

  1. 名称空间“http://www.w3.org/2005/08/addressing”未绑定到前缀“wsa”。

  2. “mustUnderstand”属性必须使用前缀“env”而不是“soapenv”。

  3. 您可以在本地声明缺少的命名空间。你只需要修改header_part看起来像这样:

    header_part = '<wsse:Security env:mustUnderstand="1" 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><wsse:Username>****</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">****</wsse:Password>'
         . '</wsse:UsernameToken></wsse:Security><wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">/IntS5/S5WS</wsa:Action>';
    

    注意:也许这很明显,但假设“http://www.w3.org/2003/05/soap-envelope”的前缀始终为“env”是危险的。框架可以随时改变这一点。如果您想要非常安全,可能更喜欢将命名空间本地绑定到新前缀:

    <wsse:Security soapenv:mustUnderstand="1" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" ...
    

    但是将一个命名空间绑定到两个不同的前缀会使事情更难以阅读和理解,所以我会说这是一个品味问题。