PHP - 没有按预期解析Soap xml

时间:2015-08-10 13:27:39

标签: php xml web-services soap wsdl

我正在使用webservices(准确地说是OTRS webservices)。我使用PHP5 soapClient和(以供将来参考)这是dump($client->__getFunctions());的结果($client是用WSDL创建的对象和web服务器端运行的URL )

array:5 [
  0 => "OTRS_TicketCreateResponse TicketCreate(OTRS_TicketCreate $TicketCreate)"
  1 => "OTRS_TicketUpdateResponse TicketUpdate(OTRS_TicketUpdate $TicketUpdate)"
  2 => "OTRS_TicketGetResponse TicketGet(OTRS_TicketGet $TicketGet)"
  3 => "OTRS_TicketSearchResponse TicketSearch(OTRS_TicketSearch $TicketSearch)"
  4 => "OTRS_SessionCreateResponse SessionCreate(OTRS_SessionCreate $SessionCreate)"
]

注意:我想拨打最后一项服务SessionCreate

同样是dump($client->__getTypes());的结果 - 仅针对我试图调用的网络服务请求(以避免一大堆文字)。

 21 => """
    struct OTRS_SessionCreate {
     string UserLogin;
     string CustomerUserLogin;
     string Password;
    }"""

WSDL文件为SessionCreate

显示了这一点
<xsd:complexType name="OTRS_SessionCreate">
        <xsd:sequence>
          <xsd:choice minOccurs="1" maxOccurs="1">
            <xsd:annotation>
              <xsd:documentation>UserLogin or CustomerUserLogin is mandatory.</xsd:documentation>
            </xsd:annotation>
            <xsd:element name="UserLogin" type="xsd:string" minOccurs="0"/>
            <xsd:element name="CustomerUserLogin" type="xsd:string" minOccurs="0"/>
          </xsd:choice>
          <xsd:element name="Password" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
      </xsd:complexType>

我的PHP代码

$client->SessionCreate("SessionCreate", array("SessionCreate" => array("UserLogin" => "Myuser","Password" => "secret")));

如果我dump($client->__getLastRequest());

<?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SessionCreate/><param1><item><key>SessionCreate</key><value><item><key>UserLogin</key><value>myuser</value></item><item><key>Password</key><value>secret</value></item></value></item></param1></SOAP-ENV:Body></SOAP-ENV:Envelope>

我实际需要发送的是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <SessionCreate>
         <!--You have a CHOICE of the next 2 items at this level-->
         <!--Optional:-->
         <UserLogin>Myuser</UserLogin>
         <Password>secret</Password>
      </SessionCreate>
   </soapenv:Body>
</soapenv:Envelope>

我的问题

我已经尝试了几种方法将数据输入soapCall - 通过数组,对象,甚至手工制作XML。我想要的是它接收带有UserLogin标签的SessionCreate标签和另一个用于密码的标签,每个标签包含数据。但无论我尝试什么,PHP总是用<param1>包裹,依此类推,并且总是有空<SessionCreate>(只需写<SessionCreate/>

所以我的两部分问题是: *如何正确地将对象提供给SOAP调用? *有没有办法可以静态声明XML并用它进行SOAP调用?即使我必须拥有$xml = "<soapEnvelope>"等等......然后只需调用类似$client->soapCall(url,xml)

的内容

1 个答案:

答案 0 :(得分:0)

我遇到了奇迹治愈的秘诀(并且我认为我将在这里分享它,以便将来需要抓住他们的网络服务故障的绝望编码员)。它是SoapParam对象。

注意:NameOfService是第一个命名服务的包装器标记。

$response = $client->__soapCall("NameOfService", array(
new SoapParam("value1", "nameOfTag"),
new SoapParam("value2","nameOfSecondTag"),
// ... so on and so forth for all the parameters you require to send
));

$ response将填充您的webservice返回的任何内容。