Parsing SOAP response

前端 未结 2 633
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 13:12

Calling a web service from my controller:

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


        
2条回答
  •  时光说笑
    2020-12-30 13:48

    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    = "".$xml."";
        // 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).

提交回复
热议问题