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个回答

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐