dsutuyxe088689 2015-08-10 13:27
浏览 59

PHP - 没有按预期解析Soap xml

I'm working with webservices (OTRS webservices to be precise). I'm using PHP5 soapClient and (for future reference) this is the result of dump($client->__getFunctions()); ($client is my object created with the WSDL and url where the webservice server side is running)

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)"
]

Note: I want to call the last service SessionCreate

Also the result of dump($client->__getTypes()); - only for the web service request I'm trying to call (to avoid a huge wall of text).

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

The WSDL file shows this for the 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>

My PHP code

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

If I do 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>

what I actually need to send is:

<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>

My question

I've tried several ways to feed the data into the soapCall - through arrays, objects, even hand crafted the XML. What I want is for it to receive a SessionCreate tag with a tag for UserLogin and another one for Password, each containing the data. But no matter what I try, PHP will always wrap with the <param1> and so on, and also always have an empty <SessionCreate> (by just writing <SessionCreate/>

So my two part question is: * How do I properly feed the object into the SOAP call? * Is there a way I can statically declare the XML and make a SOAP call with it? Even if I had to have a $xml = "<soapEnvelope>" and whatnot... and then just call something like $client->soapCall(url,xml)

  • 写回答

1条回答 默认 最新

  • doujumiao5024 2015-08-10 17:27
    关注

    I encountered The secret that's the miracle cure (and thought I'd share it here for future desperate coders in need to scratch their webservices hitch). It's the SoapParam object.

    Note: NameOfService is the first wrapper tag that names the service.

    $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 will be populated with whatever your webservice returns.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器