简体   繁体   English

PHP WSDL 响应为 object

[英]PHP WSDL response as object

I am sending a request for response to a WSDL in PHP (code below).我正在向 PHP 中的 WSDL 发送响应请求(代码如下)。 The response is structured in a way that I'm not sure how to extract data from it.响应的结构方式我不确定如何从中提取数据。 It comes back as an object with the XML inside.它以 object 的形式返回,内部带有 XML。 Is there a standard way of parsing the XML of this object?是否有解析此 object 的 XML 的标准方法?

Here is my code:这是我的代码:

<?php

    $wsdl="http://192.168.0.31/WSSmartConnect/cCustomerDocument.asmx?wsdl";

    $companyID=1;
    $sysUser='dburchfield';
    $accountNum=$_POST['accountNum'];
    $trxUniqueKey=0;
    $startDate=$_POST['startDate'];
    $endDate=$_POST['endDate'];
    $documentType=$_POST['documentType'];
    $tankNum=0;
    $serviceNum=0;
    $locationNum=0;


    $param=array(
      'pCompanyID'=>$companyID,
      'pSysUser'=>$sysUser,
      'pAccountNum'=>$accountNum,
      'pTrxUniqueKey'=>$trxUniqueKey,
      'pFromDate'=>$startDate,
      'pToDate'=>$endDate,
      'pDocumentType'=>$documentType,
      'pTankNum'=>$tankNum,
      'pServiceNum'=>$serviceNum,
      'pLocationNum'=>$locationNum
    );

    $client = new SoapClient($wsdl, array('trace' => 1));

    $soapCall = $client->LoadCustomerDocuments($param);

    //$response = $client->__getLastResponse();

    echo "<strong>" . $startDate . " - " . $endDate . "</strong><br><br>";
    var_dump($soapCall);

?> ?>

Here is the response I get.这是我得到的回应。 How do I pull the XML out and parse for keys?如何拉出 XML 并解析密钥?

C:\wamp64\www\E3Portal\smartConnect.php:85:
object(stdClass)[2]
  public 'LoadCustomerDocumentsResult' => 
    object(stdClass)[3]
      public 'any' => string '<xs:schema xmlns:mstns="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" xmlns="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="DSCustomerDocumentInfo" targetNamespace="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" attributeFormDefault="qualified" elementFormDefault="qualified"><xs:element name="DSCustomerDocumentInfo" msdata:IsDataSet="true" msdata:Us'... (length=6063)

Here is the WSDL response I get when I use SoapUI:这是我在使用 SoapUI 时得到的 WSDL 响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <LoadCustomerDocumentsResponse xmlns="http://www.addsys.com/">
         <LoadCustomerDocumentsResult>
            <xs:schema id="DSCustomerDocumentInfo" targetNamespace="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:mstns="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" xmlns="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
               <xs:element name="DSCustomerDocumentInfo" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                  <xs:complexType>
                     <xs:choice minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="CustomerDocumentInfo">
                           <xs:complexType>
                              <xs:sequence>
                                 <xs:element name="doc_type" type="xs:short" minOccurs="0"/>
                                 <xs:element name="doc_id" type="xs:int" minOccurs="0"/>
                                 <xs:element name="doc_date" type="xs:dateTime" minOccurs="0"/>
                                 <xs:element name="has_print_info" type="xs:int" minOccurs="0"/>
                                 <xs:element name="tank_num" type="xs:short" minOccurs="0"/>
                                 <xs:element name="service_num" type="xs:short" minOccurs="0"/>
                                 <xs:element name="location_num" type="xs:short" minOccurs="0"/>
                                 <xs:element name="doc_type_desc" type="xs:string" minOccurs="0"/>
                              </xs:sequence>
                           </xs:complexType>
                        </xs:element>
                     </xs:choice>
                  </xs:complexType>
               </xs:element>
            </xs:schema>
            <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
               <DSCustomerDocumentInfo xmlns="http://www.addsys.com/CustomerDocument/DSCustomerDocumentInfo.xsd">
                  <CustomerDocumentInfo diffgr:id="CustomerDocumentInfo1" msdata:rowOrder="0">
                     <doc_type>23</doc_type>
                     <doc_id>506342</doc_id>
                     <doc_date>2021-01-04T00:00:00-06:00</doc_date>
                     <has_print_info>1</has_print_info>
                     <tank_num>0</tank_num>
                     <service_num>0</service_num>
                     <location_num>1</location_num>
                     <doc_type_desc>WDMS Invoices</doc_type_desc>
                  </CustomerDocumentInfo>
                  <CustomerDocumentInfo diffgr:id="CustomerDocumentInfo2" msdata:rowOrder="1">
                     <doc_type>23</doc_type>
                     <doc_id>509603</doc_id>
                     <doc_date>2021-01-07T00:00:00-06:00</doc_date>
                     <has_print_info>1</has_print_info>
                     <tank_num>0</tank_num>
                     <service_num>0</service_num>
                     <location_num>1</location_num>
                     <doc_type_desc>WDMS Invoices</doc_type_desc>
                  </CustomerDocumentInfo>
               </DSCustomerDocumentInfo>
            </diffgr:diffgram>
         </LoadCustomerDocumentsResult>
      </LoadCustomerDocumentsResponse>
   </soap:Body>
</soap:Envelope>

It looks like your $soapCall->LoadCustomerDocumentsResult->any contains valid XML, so you can just use any of built-in XML classes:看起来您的 $soapCall->LoadCustomerDocumentsResult->any 包含有效的 XML,因此您可以使用任何内置的 XML 类:

$my_result_var = new SimpleXMLElement($soapCall->LoadCustomerDocumentsResult->any);

or或者

$reader = new XMLReader();
$reader->XML($soapCall->LoadCustomerDocumentsResult->any);
$reader->read();

Or you can try helper classes from composer - something like phpro/soap-client或者你可以尝试 composer 的辅助类——比如 phppro/soap-client

Updated: Ok, so you have a bit more complicated xml than usual, so here are some tips:更新:好的,所以你的 xml 比平常更复杂,所以这里有一些提示:

  1. to make your xml valid you need only one outer element, so要使您的 xml 有效,您只需要一个外部元素,所以
    "<xml>" . $soapCall->LoadCustomerDocumentsResult->any . "</xml>"

will fix it.会修复它。

  1. You have unusual namespace in your second element.您的第二个元素中有不寻常的名称空间。 To make it work, you need to use registerXPathNamespace function:要使其工作,您需要使用registerXPathNamespace function:
  $my_result_var = new SimpleXMLElement("<xml>" . $soapCall->LoadCustomerDocumentsResult->any . "</xml>");
  $my_result_var->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');

Next you need to get your element by namespace:接下来,您需要通过命名空间获取您的元素:

$diffgram = $my_result_var->xpath("//d:diffgram");
foreach($diffgram[0]->DSCustomerDocumentInfo->CustomerDocumentInfo as $docInfo) {
     var_dump($docInfo->doc_id);
}

Actually after $diffgram[0] you can use your second element as regular xml.实际上,在$diffgram[0]之后,您可以将第二个元素用作常规 xml。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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