Skip to content
Advertisement

PHP WSDL response as object

I am sending a request for response to a WSDL in PHP (code below). 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. Is there a standard way of parsing the XML of this object?

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?

C:wamp64wwwE3PortalsmartConnect.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:

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

Advertisement

Answer

It looks like your $soapCall->LoadCustomerDocumentsResult->any contains valid XML, so you can just use any of built-in XML classes:

$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

Updated: Ok, so you have a bit more complicated xml than usual, so here are some tips:

  1. to make your xml valid you need only one outer element, so
    "<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:
  $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.

Hope it helps.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement