简体   繁体   English

使用PHP在soap标头中格式化名称空间

[英]Formatting namespace in soap header using PHP

I need to construct an XML SOAP request header using PHP where the output looks like this: 我需要使用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>

....but my PHP is producing this instead: ....但是我的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>

Here is my PHP code: 这是我的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);

Here is the entire XML soap request I need to build: 这是我需要构建的整个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>

The two XML snippets you presented are logically the same. 您呈现的两个XML代码段在逻辑上是相同的。 Both reference the XML namespace for the AuthTokenHeader element differently, but in terms of a correctly working XML parser they express the same. 两者对AuthTokenHeader元素的XML名称空间的引用都不同,但是就正确运行的XML解析器而言,它们表示相同的名称。

You cannot change the way PHP creates the XML of a Soap request. 您无法更改PHP创建Soap请求的XML的方式。 If the created request does not work with the server you have to use, please be more specific what problems you encounter. 如果创建的请求不适用于您必须使用的服务器,请更具体地说明您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM