如何在发送之前更改SOAP请求中的名称空间前缀(皇家邮件 - 发送请求)

时间:2014-10-07 09:55:55

标签: php xml api soap

我无法弄清楚如何在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>

2 个答案:

答案 0 :(得分:1)

显然,不需要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));

答案 1 :(得分:0)

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

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

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