【问题标题】:How can I change the namespace prefixes in a SOAP request before sending ( Royal Mail - ship request )如何在发送之前更改 SOAP 请求中的命名空间前缀(皇家邮政请求)
【发布时间】:2014-12-01 17:19:10
【问题描述】:

我不知道如何在 SOAP 请求中获取所需的前缀。

SOAP-ENV 应该是soapenv

ns1 应该是v11

ns2 应该是v1

ns3 应该是oas

在发送请求之前如何替换它们?

    $client = new SoapClient("schema/ShippingAPI_V1.0.wsdl", array('soap_version' => SOAP_1_1) );
    $client->__setLocation("https://api.royalmail.com/shipping/onboarding");

    $HeaderObjectXML  = '<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-000">
                  <wsse:Username>user</wsse:Username>
                  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">pass</wsse:Password>
                  <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">nonce</wsse:Nonce>
                  <wsu:Created>'.$time.'</wsu:Created>
               </wsse:UsernameToken>
           </wsse:Security>';

    $HeaderObject = new SoapVar( $HeaderObjectXML, XSD_ANYXML );

    $header = new SoapHeader( 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Security', $HeaderObject );
    $client->__setSoapHeaders($header);

    $request = Array(

        'integrationHeader' => Array(
            'dateTime' => $time,
            'version' => '1.0',
            'identification' => Array(
                'applicationId' => 'appId',
                'transactionId' => 'transId'
            )
        ),

        'requestedShipment' => Array(

        )
    );

    echo print_r($request);

    //get response
    $resp = $client->__soapCall( 'createShipment', array($request) );

这就是创建的内容:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:ns1="http://www.royalmailgroup.com/integration/core/V1"
                       xmlns:ns2="http://www.royalmailgroup.com/api/ship/V1"
                       xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <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-000">
                    <wsse:Username>user</wsse:Username>
                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">pass</wsse:Password>
                    <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">nonce</wsse:Nonce>
                    <wsu:Created>2014-10-01T14:15:50</wsu:Created>
                </wsse:UsernameToken>
            </wsse:Security>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns2:createShipmentRequest>
                <ns2:integrationHeader>
                    <ns1:dateTime>2014-10-01T14:15:50</ns1:dateTime>
                    <ns1:version>1.0</ns1:version>
                    <ns1:identification>
                        <ns1:applicationId>appId</ns1:applicationId>
                        <ns1:transactionId>transId</ns1:transactionId>
                    </ns1:identification>
                </ns2:integrationHeader>
                <ns2:requestedShipment>
                    ...
                </ns2:requestedShipment>
            </ns2:createShipmentRequest>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

但应该是:

   <soapenv:Envelope xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:v1="http://www.royalmailgroup.com/api/ship/V1"
                        xmlns:v11="http://www.royalmailgroup.com/integration/core/V1">
       <soapenv: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-000">
                  <wsse:Username>user</wsse:Username>
                  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">pass</wsse:Password>
                  <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">nonce</wsse:Nonce>
                  <wsu:Created>2014-09-02T13:31:29.934Z</wsu:Created>
               </wsse:UsernameToken>
           </wsse:Security>
       </soapenv:Header>

       <soapenv:Body>
          <v1:createShipmentRequest>
             <v1:integrationHeader>
                <v11:dateTime>2014-08-14T14:19:42</v11:dateTime>
                <v11:version>1.0</v11:version>
                <v11:identification>
                   <v11:applicationId>appId</v11:applicationId>
                   <v11:transactionId>transId</v11:transactionId>
                </v11:identification>
             </v1:integrationHeader>
             <v1:requestedShipment>
                ...
             </v1:requestedShipment>
          </v1:createShipmentRequest>
       </soapenv:Body>
    </soapenv:Envelope>

WSDL 文件:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!--
    ########################################################################################################
    #  Description: Royal Mail Shipping API Public Facing WSDL
    #  Revision:    1.0
    #  Date:        01/05/2014
    ########################################################################################################
    #  History:
    #  Version      Date            Author                           Description of Change
    #  v0.1         24/06/2013      Somsubhra Bhattacharya(CSC)      Initial Draft
    #  v0.2         29/06/2013      Somsubhra Bhattacharya(CSC)      Based on XSD V0.2
    #  v0.3         02/07/2013      Somsubhra Bhattacharya(CSC)      Based on XSD V0.3
    #  v0.4         19/08/2013      Somsubhra Bhattacharya(CSC)      Based on XSD V0.4
    #  v0.5         16/12/2013      Somsubhra Bhattacharya(CSC)      Incorporated PCR40 
    #  v0.6         04/02/2014      Somsubhra Bhattacharya(CSC)      Aligned with v0.6 of XSD 
    #  v0.7         03/04/2014      Somsubhra Bhattacharya(CSC)      Aligned with v0.7 of XSD
    #  v0.8         17/04/2014      Somsubhra Bhattacharya(CSC)      Aligned with v0.8 of XSD
    #  v1.0         01/05/2014      Somsubhra Bhattacharya(CSC)      Baselined version 1.0
    ########################################################################################################
    -->
    <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:tns="http://www.royalmailgroup.com/api/ship/V1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="shippingAPI"
        xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
        targetNamespace="http://www.royalmailgroup.com/api/ship/V1">
    <wsdl:documentation>Version: 1.0</wsdl:documentation>   
        <wsdl:types>
            <xs:schema attributeFormDefault="unqualified"
                elementFormDefault="qualified" targetNamespace="http://www.royalmailgroup.com/WSDLapi/ship/V1" xmlns:tns="http://www.royalmailgroup.com/WSDLapi/ship/V1"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:import namespace="http://www.royalmailgroup.com/api/ship/V1" schemaLocation="ShippingAPI_V1.0.xsd" />
                <xs:import namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" schemaLocation="oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
            </xs:schema>
        </wsdl:types>
        <wsdl:message name="createShipmentRequest">
            <wsdl:part name="createShipmentRequest" element="tns:createShipmentRequest" />
        </wsdl:message>
        <wsdl:message name="createShipmentResponse">
            <wsdl:part name="createShipmentResponse" element="tns:createShipmentResponse" />
        </wsdl:message>
        <wsdl:message name="printLabelRequest">
            <wsdl:part name="printLabelRequest" element="tns:printLabelRequest" />
        </wsdl:message>
        <wsdl:message name="printLabelResponse">
            <wsdl:part name="printLabelResponse" element="tns:printLabelResponse" />
        </wsdl:message>
        <wsdl:message name="createManifestRequest">
            <wsdl:part name="createManifestRequest" element="tns:createManifestRequest" />
        </wsdl:message>
        <wsdl:message name="createManifestResponse">
            <wsdl:part name="createManifestResponse" element="tns:createManifestResponse" />
        </wsdl:message>
        <wsdl:message name="printManifestRequest">
            <wsdl:part name="printManifestRequest" element="tns:printManifestRequest" />
        </wsdl:message>
        <wsdl:message name="printManifestResponse">
            <wsdl:part name="printManifestResponse" element="tns:printManifestResponse" />
        </wsdl:message>
        <wsdl:message name="updateShipmentRequest">
            <wsdl:part name="updateShipmentRequest" element="tns:updateShipmentRequest" />
        </wsdl:message>
        <wsdl:message name="updateShipmentResponse">
            <wsdl:part name="updateShipmentResponse" element="tns:updateShipmentResponse" />
        </wsdl:message>
        <wsdl:message name="cancelShipmentRequest">
            <wsdl:part name="cancelShipmentRequest" element="tns:cancelShipmentRequest" />
        </wsdl:message>
        <wsdl:message name="cancelShipmentResponse">
            <wsdl:part name="cancelShipmentResponse" element="tns:cancelShipmentResponse" />
        </wsdl:message>
        <wsdl:message name="securityHeader">
            <wsdl:part name="security" element="wsse:Security"/>
        </wsdl:message>
        <wsdl:message name="faultResponse">
             <wsdl:part name="faultResponse" element="tns:exceptionDetails" />
        </wsdl:message>
        <wsdl:portType name="shippingAPIPortType">
            <wsdl:operation name="createShipment" parameterOrder="createShipmentRequest">
                <wsdl:input message="tns:createShipmentRequest" />
                <wsdl:output message="tns:createShipmentResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
            <wsdl:operation name="printLabel" parameterOrder="printLabelRequest">
                <wsdl:input message="tns:printLabelRequest" />
                <wsdl:output message="tns:printLabelResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
            <wsdl:operation name="createManifest" parameterOrder="createManifestRequest">
                <wsdl:input message="tns:createManifestRequest" />
                <wsdl:output message="tns:createManifestResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
            <wsdl:operation name="printManifest" parameterOrder="printManifestRequest">
                <wsdl:input message="tns:printManifestRequest" />
                <wsdl:output message="tns:printManifestResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
            <wsdl:operation name="updateShipment" parameterOrder="updateShipmentRequest">
                <wsdl:input message="tns:updateShipmentRequest" />
                <wsdl:output message="tns:updateShipmentResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
            <wsdl:operation name="cancelShipment" parameterOrder="cancelShipmentRequest">
                <wsdl:input message="tns:cancelShipmentRequest" />
                <wsdl:output message="tns:cancelShipmentResponse" />
                <wsdl:fault name="faultResponse" message="tns:faultResponse" />
            </wsdl:operation>
        </wsdl:portType>
        <wsdl:binding name="shippingAPISoapBinding" type="tns:shippingAPIPortType">
            <soap:binding style="document"
                transport="http://schemas.xmlsoap.org/soap/http" />
            <wsdl:operation name="createShipment">
                <soap:operation soapAction="createShipment" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>
            </wsdl:operation>
            <wsdl:operation name="printLabel">
                <soap:operation soapAction="printLabel" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>
            </wsdl:operation>
            <wsdl:operation name="createManifest">
                <soap:operation soapAction="createManifest" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>           
            </wsdl:operation>
            <wsdl:operation name="printManifest">
                <soap:operation soapAction="printManifest" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>           
            </wsdl:operation>
            <wsdl:operation name="updateShipment">
                <soap:operation soapAction="updateShipment" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>           
            </wsdl:operation>
            <wsdl:operation name="cancelShipment">
                <soap:operation soapAction="cancelShipment" style="document" />
                <wsdl:input>
                    <soap:header message="tns:securityHeader" part="security" use="literal"/>
                    <soap:body use="literal" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal" />
                </wsdl:output>
                <wsdl:fault name="faultResponse">
                    <soap:fault name="faultResponse" use="literal"/>
                </wsdl:fault>           
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:service name="shippingAPI">
            <wsdl:port binding="tns:shippingAPISoapBinding" name="shippingAPIPort">
                <soap:address location="https://api.royalmail.com/shipping/" />
            </wsdl:port>
        </wsdl:service>
    </wsdl:definitions>

【问题讨论】:

  • 我发现this SO 问题与您的问题相似,并且在评论中建议命名空间并不重要,并且接收 SOAP 的 API 端点有问题。
  • 命名空间 PREFIX 不相关。它只是命名空间的本地别名。因此,如果读取 XML 的内容依赖于特定的命名空间前缀 - 它就会被破坏。命名空间本身是相关的,它是标识节点所属 XML 标准的唯一字符串。
  • 不管命名空间不重要和API被破坏,这个前缀可以在发送请求之前更改吗?

标签: php xml api soap


【解决方案1】:

您应该向 SoapClient 添加证书,如下所示:

$local_cert = FULL_PATH_TO_CERT;
$client = new SoapClient($wsdl, array(
            'local_cert' => $local_cert,
            'passphrase' => $passphrase
        ));

我用这个解决了我的问题,但现在我有其他问题。当我发送请求时,它返回授权失败。

【讨论】:

    【解决方案2】:

    显然,v1、v11 和 oas 命名空间不是必需的。您可以通过身份验证并通过 ns1、ns2 和 ns3 发出有效请求。

    一段时间以来,我遇到授权失败,使用以下方法使其正常工作。

    $created = gmdate('Y-m-d\TH:i:s\Z');
    $passwordDigest = base64_encode(sha1($nonce . $created . base64_encode(sha1("Password2014!", true)), true));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多