使用PHP格式化soap标头中的命名空间

时间:2014-07-01 23:14:41

标签: php xml soap

我需要使用PHP构建XML SOAP请求标头,其输出如下所示:

            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <soap:Header>
            <AuthTokenHeader xmlns="SPP.net/ContractData">
            <Token>B1912A8BA7AB6E56D688244D7B</Token> 
            </AuthTokenHeader>
            </soap:Header>

....但我的PHP正在制作它:

            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="SPP.net/ContractData">
            <SOAP-ENV:Header>
            <ns1:AuthTokenHeader>
            <Token>373161D1C28</Token>
            </ns1:AuthTokenHeader>
            </SOAP-ENV:Header>

这是我的PHP代码:

                // define namespace
                $NameSpace = 'SPP.net/ContractData';

                // build AuthTokenHeader vars
                $authTokenVar[] = new SoapVar($TOKEN, XSD_STRING, null, null, 'Token');

                // wrap in AuthTokenHeader tags
                $header = new SoapVar($authTokenVar, SOAP_ENC_OBJECT, null, null, 'AuthTokenHeader');

                // build soap header
                $soapHeader = new SoapHeader($NameSpace, 'AuthTokenHeader', $header , false);

                // instanciate soap client
                $wsdlUrl= "https://gmwstest.sppinc.net/VPP/ContractExchange.asmx?wsdl";
                $client = new SoapClient($wsdlUrl,
                array(
                "trace"      => 1,
                "exceptions" => 0,
                "cache_wsdl" => 0));

                // set soap headers
                $client->__setSoapHeaders($soapHeader);

以下是我需要构建的整个XML soap请求:

            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <soap:Header>
            <AuthTokenHeader xmlns="SPP.net/ContractData">
            <Token>BEB40F7FD43168017ACF3772A91F2B7C2B37A722F6421598D0B61CCD3DBF8928CA18B25970AB6D43FD31A761F15D84A52C9FC315BE9F708F30EE4F08DAB4C74F94894A7D3242102831FB8303684F7FE492A2249CB35FB50030C905DD91F771103C5A1D486CCEA9AAB0D42AC37252B2E350B4E3F24CEE8D0B8CFCE04F1DD7D1B4D7C2A4F1912A8BA7AB6E56D688244D7B</Token> <!--GET TOKEN FROM LOGONRESULT SESSION VARIABLE-->
            </AuthTokenHeader>
            </soap:Header>
            <soap:Body>
            <SendContractData xmlns="SPP.net/ContractData">
            <contract>
            <ContractID>VPP00253221</ContractID> <!--GET ContractID FROM CONTRACT RESPONSE SESSION VARIABLE-->
            <AccountNumber/>
            <Status>Pending</Status>
            <Company>VPP</Company>
            <ContractPrefix/>
            <DealerCode>10456</DealerCode>
            <FirstName>FirstName</FirstName>
            <Mi/>
            <LastName>Lastname</LastName>
            <PhoneNumber>(312)980-6000</PhoneNumber>
            <Address1>303 Anytown Lane</Address1>
            <Address2>Unit A</Address2>
            <City>Yourtown</City>
            <StateProvinceAbbr xsi:type="StateEnum">IL</StateProvinceAbbr>
            <ZipCode>60601</ZipCode>
            <Country>USA</Country>
            <OriginatingDepartment>Other</OriginatingDepartment>
            <Language>English</Language>
            <VIN>1HGCM56705AJMD227</VIN>
            <PurchasePrice>2000.00</PurchasePrice>
            <SalesTax>0.00</SalesTax>
            <DownPayment>200.00</DownPayment>
            <AmountFinanced>1800.00</AmountFinanced>
            <PolicyType>ServiceContract</PolicyType>
            <InServiceDate>2010-03-12T00:00:00</InServiceDate>
            <FirstPaymentDate xsi:nil="true"/>
            <DealerCost>500</DealerCost>
            <ContractHoldersInitialsCoordinates xsi:nil="true"/>
            <ContractHoldersInitialsDateCoordinates xsi:nil="true"/>
            <ContractHoldersSignatureCoordinates xsi:nil="true"/>
            <SignatureDateCoordinates xsi:nil="true"/>
            <SellersSignatureCoordinates xsi:nil="true"/>
            <PurchaseDate>2014-06-23T14:24:46</PurchaseDate>
            <BeginMileage>43650</BeginMileage>
            <TermMonths>84</TermMonths>
            <TermMileage>100000</TermMileage>
            <NumberOfPayments>12</NumberOfPayments>
            <CallBackURL>http://NewWarrantyVehicle_Example/Success.php</CallBackURL>
            </contract>
            </SendContractData>
            </soap:Body>
            </soap:Envelope>

1 个答案:

答案 0 :(得分:2)

您提供的两个XML代码段在逻辑上是相同的。两者都以不同方式引用AuthTokenHeader元素的XML名称空间,但就正确工作的XML解析器而言,它们表达相同。

您无法更改PHP创建Soap请求的XML的方式。如果创建的请求不能与您必须使用的服务器一起使用,请更具体地说明您遇到的问题。