通过PHP SoapClient请求发送原始XML

时间:2012-03-07 20:08:39

标签: php soap nusoap

我试图通过PHP和SoapClient简单地将RAW xml发送到web服务。问题是当我对XML进行编码时,它会改变XML中转换为关联数组的元素顺序。

// Initialize the Soap Client:
$this->_transactionServicesClient = new SoapClient($soapWSDLUrl);

将以下XML作为字符串发送到SoapClient的最佳方法是什么或者什么是什么?

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.micros.com/pos/les/TransactionServices">
    <SOAP-ENV:Body>
        <ns1:PostTransaction>
            <ns1:REQ>
                <ns1:RequestHeader>
                    <ns1:InterfaceVersion>3.0.7</ns1:InterfaceVersion>
                    <ns1:ClientName>TRANS_SERVICES</ns1:ClientName>
                </ns1:RequestHeader>    
                <ns1:CheckDetailEntries>
                    <ns1:MenuItem>
                        <ns1:ReferenceEntry>Pizza4</ns1:ReferenceEntry>
                        <ns1:Count>1</ns1:Count>
                        <ns1:Price>10.00</ns1:Price>
                        <ns1:ItemNumber>112001</ns1:ItemNumber>
                        <ns1:PriceLevel>1</ns1:PriceLevel>
                        <ns1:Seat xsi:nil="true"/>
                    </ns1:MenuItem>
                </ns1:CheckDetailEntries>
                <ns1:CheckHeaderRequest>
                    <ns1:CheckId>03:21:05.050505</ns1:CheckId>
                    <ns1:GuestCount>1</ns1:GuestCount>
                    <ns1:GuestInformation>
                    <ns1:ID>001</ns1:ID>
                    <ns1:FirstName>xxx</ns1:FirstName>
                    <ns1:LastName>xxx</ns1:LastName>
                    <ns1:Address1>xxx Rd</ns1:Address1>
                    <ns1:Address2>xx</ns1:Address2>
                    <ns1:Address3>xx</ns1:Address3>
                    <ns1:PhoneNum>xx</ns1:PhoneNum>
                    <ns1:UserText1>None</ns1:UserText1>
                    <ns1:UserText2>None</ns1:UserText2>
                    <ns1:UserText3>None</ns1:UserText3>
                    <ns1:GUID></ns1:GUID></ns1:GuestInformation>
                </ns1:CheckHeaderRequest>
                <ns1:OrderTypeNumber>1</ns1:OrderTypeNumber>
            </ns1:REQ>
        </ns1:PostTransaction>
    </SOAP-ENV:Body>        
</SOAP-ENV:Envelope>

  

更新/解决方案:以下是我用于扩展SOAP客户端并发送原始肥皂信封的代码:My answer below

2 个答案:

答案 0 :(得分:8)

  

更新/解决方案:以下是我用来扩展SOAP客户端并发送原始肥皂信封的代码

以下是我如何扩展SoapClient:

<?php
class MySoapClient extends SoapClient {

    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
        $this->server = new SoapServer($wsdl, $options);
    }
    public function __doRequest($request, $location, $action, $version) 
    { 
        $result = parent::__doRequest($request, $location, $action, $version); 
        return $result; 
    } 
    function __myDoRequest($array,$op) { 
        $request = $array;
        $location = 'http://xxxxx:xxxx/TransactionServices/TransactionServices6.asmx';
        $action = 'http://www.micros.com/pos/les/TransactionServices/'.$op;
        $version = '1';
        $result =$this->__doRequest($request, $location, $action, $version);
        return $result;
    } 
}

// To invoke my new custom method with my Soap Envelope already prepared.
$soapClient = new MySoapClient("http://xxxx:xxxx/TransactionServices/TransactionServices6.asmx?WSDL", array("trace" => 1)); 
$PostTransaction = $soapClient->__myDoRequest($orderRequest,$op); 
?>

在我将其转化为答案之前,也发布在pastie.org上:http://pastie.org/3687935

答案 1 :(得分:4)

出于测试目的,您可以继承SoapClient并覆盖__doRequest方法 - 它接收生成的XML并且您可以预处理它。

但更好地发现XML编码出了什么问题。您可以使用SoapVar和SoapParam实例来指定给定对象必须表示的确切方式。当必须给出命名空间时,那些节省了我的生命