简体   繁体   中英

using stdClass() to create soap request wsdl complex type

After finding PHP soap does not handle arrays with a complex type, I'm now using stdClass() I've coded something but it creates the soap request in the wrong sequence. I want the idtag and idtaginfo to be in pairs like below.

     <soap:Body>
     <ns:sendLocalListRequest>
     <ns:updateType>full</ns:updateType>
     <ns:listVersion>1</ns:listVersion>

     <ns:localAuthorisationList>

           <ns:idTag>1</ns:idTag>
                   <ns:idTagInfo>
                      <ns:status>good</ns:status>
                    </ns:idTagInfo>

            <ns:idTag>2</ns:idTag>
                   <ns:idTagInfo>
           <ns:status>bad</ns:status>
                    </ns:idTagInfo>

     </ns:localAuthorisationList>

  </ns:sendLocalListRequest>
   </soap:Body>

my soap request which is wrong is coming out like the below

  <env:Body><ns1:sendLocalListRequest><ns1:updateType>FULL</ns1:updateType>
  <ns1:listVersion>1</ns1:listVersion>

  <ns1:localAuthorisationList>
  <ns1:idTag>1</ns1:idTag><ns1:idTag>2</ns1:idTag>

 <ns1:idTagInfo><ns1:status>good</ns1:status><ns1:status>bad</ns1:status>
  </ns1:idTagInfo>
   </ns1:localAuthorisationList></ns1:sendLocalListRequest></env:Body>

this is relevant part of the wsdl

 <s:complexType name="SendLocalListRequest">
    <s:annotation>
      <s:documentation>Defines the SendLocalList.req PDU</s:documentation>
    </s:annotation>
    <s:sequence>
      <s:element name="updateType" type="tns:UpdateType" minOccurs="1" maxOccurs="1" />
      <s:element name="listVersion" type="s:int" minOccurs="1" maxOccurs="1" />
      <s:element name="localAuthorisationList" type="tns:AuthorisationData"
       minOccurs="1" maxOccurs="unbounded" />
      <s:element name="hash" type="s:string" minOccurs="0" maxOccurs="1" />
    </s:sequence>
  </s:complexType>

    <s:complexType name="AuthorisationData">
          <s:sequence>
       <s:element name="idTag" type="tns:string" minOccurs="1" maxOccurs="unbounded"/>
      <s:element name="idTagInfo" type="tns:IdTagInfo" minOccurs="1"
     maxOccurs="unbounded"/>
            </s:sequence>
                 </s:complexType>

  <s:simpleType name="UpdateType">
    <s:restriction base="s:string">
      <s:enumeration value="Differential"/>
      <s:enumeration value="Full"/>
    </s:restriction>
  </s:simpleType>

 <s:complexType name="SendLocalListResponse">
    <s:annotation>
      <s:documentation>Defines the SendLocalList.conf PDU</s:documentation>
    </s:annotation>
    <s:sequence>
      <s:element name="status" type="tns:UpdateStatus" minOccurs="1" maxOccurs="1" />
      <s:element name="hash" type="s:string" minOccurs="0" maxOccurs="1" />
    </s:sequence>
  </s:complexType>

 <s:complexType name="IdTagInfo">
    <s:sequence>
      <s:element name="status" type="s:string" minOccurs="1" maxOccurs="unbounded" />
      <s:element name="expiryDate" type="s:dateTime" minOccurs="0" maxOccurs="1"/>
      <s:element name="parentIdTag" type="tns:IdToken" minOccurs="0" maxOccurs="1"/>
    </s:sequence>
  </s:complexType>

This my code

$search_query = new StdClass();
$search_query->updateType = $updatetype;
$search_query->listVersion = $listversion;

 $search_query->localAuthorisationList = new StdClass();

 while ($row = $db->getResult()) {


 $search_query->localAuthorisationList->idTag[] = $row['rfid'];

 $search_query->localAuthorisationList->idTagInfo->status[] = $row['status'];

 }


   ini_set("soap.wsdl_cache_enabled", "0"); 

$path        = realpath($_SERVER["DOCUMENT_ROOT"]);
$endpoint    = $wsdl;
$soapOptions = array(    'exceptions'     => 0
                        ,'soap_version'   => SOAP_1_2
                        ,'trace'          => true
                                                                    ,'uri' => $theversion
                        ,'location' => $url
                                                    ); 

$header = new SoapHeader($theversion,'', $ppid);

$client->__setSoapHeaders($header);  

    $response = $client->SendLocalList($search_query);

Update

I've got close but still falling at the last hurdle the following will produce the list

while ($row2 = $db->getResult()) { 
$search_query[] = new  SoapStructAuthorisationData($row2['rfid'],array(new 
SoapStructIdTagInfo('ConcurrentTx'))); 
}

// implode with commas and remove last comma

$search_list=implode(', ', $search_query);

$search_list_nocommaend = rtrim($search_list, ', ');

a var_dump of this produces SoapStructAuthorisationData, SoapStructAuthorisationData, SoapStructAuthorisationData, SoapStructAuthorisationData, SoapStructAuthorisationData

if I enclose the code above in speech marks like "new SoapStructAuthorisationData($row2['rfid'],array(new SoapStructIdTagInfo('ConcurrentTx')))"; is will produce the whole thing but this wont show when I do the following

  if($soapServiceSend->SendLocalList(new SoapStructSendLocalListRequest($updateType,
     $listversion, 
  array( $search_list_nocommaend ))))

if I do something like this for a test piece it works

$search=new SoapStructAuthorisationData('BUKIEE',array(new 
SoapStructIdTagInfo('ConcurrentTx')));

Any ideas anyone?

I've faced a lot of times this type of problems and I've found the easiest solution: http://www.wsdltophp.com/

Here you upload your WSDL and it will create all the classes needed to communicate with the server.

Try it, and you maybe will find that you don't have to be dealing with the creation of the classes by yourself.

Ps. I've found that sometimes the data must not be sent as array, so in the general options unmark " Send An Array As Parameter "

EDIT - Code from Wsdl2PHP

<?php
$soap = new Saop2StructSendLocalListRequest(); 
$soap->SendLocalList(
    new Saop2StructSendLocalListRequest(
        $updateType, 
        $listversion, 
        array (
            new Saop2StructAuthorisationData(), 
            new Saop2StructAuthorisationData(), 
            new Saop2StructAuthorisationData()
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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