使用stdClass()创建soap请求wsdl复杂类型


using stdClass() to create soap request wsdl complex type

发现PHP soap不处理具有复杂类型的数组后,我现在使用stdClass(),我已经编码了一些东西,但它以错误的顺序创建了soap请求。我希望idtag和idtaginfo像下面这样成对。

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

我的soap请求是错误的,输出如下

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

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

我的代码

$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);

更新

我已经接近了,但仍然在最后一个障碍,下面将产生列表

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, ', ');

此var_dump产生SoapStructAuthorisationData、SoapStructAuthorisationData、SoapStructAuthorisationData、SoapStructAuthorisationData、SoapStructAuthorisationData

如果我将上面的代码括在像"new SoapStructAuthorisationData($row2['rfid'],array(newSoapStructIdTagInfo (ConcurrentTx)))";它将生成整个内容,但当我执行以下命令

时,它不会显示出来
  if($soapServiceSend->SendLocalList(new SoapStructSendLocalListRequest($updateType,
     $listversion, 
  array( $search_list_nocommaend ))))

如果我对测试件做这样的操作它会起作用

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

有什么想法吗?

我遇到过很多次这类问题,我找到了最简单的解决方案:http://www.wsdltophp.com/

您在这里上传WSDL,它将创建与服务器通信所需的所有类。

尝试一下,你可能会发现你不必自己处理类的创建。

p。我发现有时数据不能作为数组发送,所以在一般选项中取消标记"发送数组作为参数"

EDIT - Code from Wsdl2PHP

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