解析 SOAP 响应

时间:2023-02-22
本文介绍了解析 SOAP 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

从我的控制器调用网络服务:

Calling a web service from my controller:

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

我明白了:

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

推荐答案

您不太清楚使用"是什么,但您显然需要某种形式的 XML 解析/搜索.

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

例如,尝试 xml 加载该字符串和 var_dump 结果.简单地列举各种属性应该会向您展示机会.

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

稍后,您可能会尝试XPath 搜索 和更高级的技巧"以加快工作速度.

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: 
";
    foreach($rooms as $i => $room)
    {
            print "Room {$i}: id={$room['id']} (official id: {$room->IdHabitacion}
";
            print "Entrada {$room->FechaEntrada}, salida {$room->FechaSalida}
...
";
    }

您可以使用多种解析器,这是一个快速而肮脏的解析器.

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

在此处查看更多信息.

注意:对于非常大的 XML 数据集,我发现 foreach 最好.

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

对于只需要少量信息并且整个文件可能不适合可用内存的大型数据集,您可能需要使用 XMLParser 或 XMLReader,并在保持/操作的同时通过解析器筛选整个文件(例如发送到数据库中,或显示到HTML) 仅提供您需要的信息.

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.

虽然这不是一般的好习惯,但您可以在进入一个长的 XML 解析循环之前关闭输出缓冲,一旦得到它就输出 HTML 并偶尔刷新().这会将 HTML 外包给 HTTP 服务器,在 PHP 进程中占用更少的内存,代价是压缩稍差(如果你输出超过大约 40K 的 HTML 块,差异可以忽略不计)和成比例地更好的响应性(用户看到"某些事情发生得更快,即使整体操作完成需要更长的时间.体验是更快加载).

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

这篇关于解析 SOAP 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:SoapClient 设置自定义 HTTP Header 下一篇:如何在 PHP 中使用 SSL 证书发送 SOAP 请求?

相关文章

最新文章