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

时间:2013-03-01 14:36:26

标签: php soap

在发现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>

我的肥皂请求出现错误,如下所示

  <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(new)这样的语音标记中     SoapStructIdTagInfo('ConcurrentTx')))“;会生成整个东西但是当我执行以下操作时不会显示

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

如果我为测试片做了类似的事情,那就可以了。

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

任何想法?

1 个答案:

答案 0 :(得分:1)

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

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

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

聚苯乙烯。我发现有时数据不能作为数组发送,因此在常规选项中取消标记“将数组作为参数发送

编辑 - 来自Wsdl2PHP的代码

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