8

Calling a web service from my controller:

$client = new \SoapClient("http://.../webservice/NAME_OF_PAGE.asmx?WSDL");
$result = $client->EstadoHabitacionesFechas();

I get this:

<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="TablaEstadoHabitacion" msdata:UseCurrentLocale="true">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="TablaEstadoHabitacion">
                    <xs:complexType><xs:sequence>
                        <xs:element name="IdHabitacion" type="xs:int" minOccurs="0"/>
                        <xs:element name="FechaEntrada" type="xs:string" minOccurs="0"/>
                        <xs:element name="FechaSalida" 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">
    <DocumentElement xmlns="">
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
            <IdHabitacion>1</IdHabitacion>
            <FechaEntrada>23/05/2012</FechaEntrada>
            <FechaSalida>31/12/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
            <IdHabitacion>2</IdHabitacion>
            <FechaEntrada>23/05/2012</FechaEntrada>
            <FechaSalida>29/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
            <IdHabitacion>2</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
            <IdHabitacion>3</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion5" msdata:rowOrder="4" diffgr:hasChanges="inserted">
            <IdHabitacion>3</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion6" msdata:rowOrder="5" diffgr:hasChanges="inserted">
            <IdHabitacion>4</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion7" msdata:rowOrder="6" diffgr:hasChanges="inserted">
            <IdHabitacion>5</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion20" msdata:rowOrder="19" diffgr:hasChanges="inserted">
            <IdHabitacion>10</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
    </DocumentElement>
</diffgr:diffgram>

How can I parse this data and use it?

LSerni
  • 55,617
  • 10
  • 65
  • 107
Kioko Kiaza
  • 1,378
  • 8
  • 28
  • 57
  • How is this different from you earlier [question](http://stackoverflow.com/questions/11310476/soapclient-parse-result)? – bluevector Jul 03 '12 at 20:13

2 Answers2

14

You don't make very clear what "use" is, but you clearly need some form of XML parsing/search.

For example, try xml-loading that string and var_dump the result. Simply enumerating the various properties should show you the opportunities.

Later on, you might try XPath search and more advanced "tricks" to speed up the work.

    // Remove namespaces
    $xml    = str_replace(array("diffgr:","msdata:"),'', $xml);
    // Wrap into root element to make it standard XML
    $xml    = "<package>".$xml."</package>";
    // Parse with SimpleXML - probably there're much better ways
    $data   = simplexml_load_string($xml);
    $rooms  = $data->package->diffgram->DocumentElement->TablaEstadoHabitacion;
    print "We have " . count($rooms) . " rooms: \n";
    foreach($rooms as $i => $room)
    {
            print "Room {$i}: id={$room['id']} (official id: {$room->IdHabitacion}\n";
            print "Entrada {$room->FechaEntrada}, salida {$room->FechaSalida}\n...\n";
    }

There are several parsers you can use, this is a quick and dirty one.

See more here.

Large data sets

Note: for very large XML data sets, I've found out that foreach is best.

And for large data sets where you only need a few information, and the whole file might not fit into available memory, you will probably want to use XMLParser, or XMLReader, and sift the whole file through the parser while keeping/manipulating (e.g. sending in a DB, or displaying to HTML) only the information you need.

While this isn't in general good practice, you can turn output buffering off before entering a long XML parsing loop, outputting HTML as soon as you have it and flush()ing once in a while. This will outsource the HTML to the HTTP server, taking up less memory in the PHP process, at the expense of slightly inferior compression (if you output chunks of HTML of more than about 40K, the difference is negligible) and proportionally better responsivity (the user "sees" something happen faster, even if overall operation completion takes a little longer. The experience is that of a faster load).

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
2

Complete Reference Code To Soap Response XML File Here How To parse we will see in below example code

<?xml version="1.0" encoding="utf-8"?>
    <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>
            <xyzResponse xmlns="http://tempuri.org/">
                <xyzResult>
                    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="Table">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="myCityID" type="xs:int" minOccurs="0" />
                                                <xs:element name="myCityName" type="xs:string" minOccurs="0" />

                                                <xs:element name="myLat" type="xs:double" minOccurs="0" />
                                                <xs:element name="myLon" type="xs:double" 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">
                        <NewDataSet xmlns="">
                            <Table diffgr:id="Table1" msdata:rowOrder="0">
                                <CityID>1</CityID>
                                <CityName>Ahmedabad</CityName>
                                <Lat>23.045839</Lat>
                                <Lon>72.550578</Lon>
                            </Table>
                            <Table diffgr:id="Table2" msdata:rowOrder="1">
                                <CityID>21</CityID>
                                <CityName>Amritsar</CityName>
                                <Lat>31.705603</Lat>
                                <Lon>74.807337</Lon>
                            </Table>
                        </NewDataSet>
                    </diffgr:diffgram>
                </xyzResult>
            </xyzResponse>
        </soap:Body>
    </soap:Envelope>

using this code you can easily get all the Tables data in $title variable and using that you can easily get any specific value easily...

    <?php
        ini_set('soap.wsdl_cache_enabled', 0);
        ini_set('soap.wsdl_cache_ttl', 900);
        ini_set('default_socket_timeout', 15);

        $apiauth =array('UserName'=>'USENAME','Password'=>'PASSWORD','UserCode'=>'1991');
        $wsdl = 'http://SITENAME.com/service.asmx?WSDL';
        try {

            $soap = new SoapClient($wsdl);
            $header = new SoapHeader('http://tempuri.org/', 'AuthHeader', $apiauth);
            $soap->__setSoapHeaders($header);   
            $data = $soap->methodname($header);
        }
        catch(Exception $e) {
            die($e->getMessage());
        }
        // echo "<pre>";
        // print_r($data->xyzResult->any);
        $response = $data->xyzResult->any;
        $sxe = new SimpleXMLElement($response);
        $sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
        $result = $sxe->xpath("//NewDataSet");
        echo "<pre>";
        foreach ($result[0] as $title) {
            print_r($title);
        }
        die;
    ?>
Hina Vaja
  • 314
  • 1
  • 5
  • 15
  • The line $response = $data->xyzResult->any was resulting in an error because of the XML format within the SimpleXMLElement. Instead I activated trace in the SoapClient constructor and used the $soap->__getLastResponse() and it worked. Thanks a lot, you saved my day! – André Luiz Müller Jun 14 '17 at 18:48