analysieren eine SOAP-XML-Antwort mit der Verwendung von PHP-Namespaces
Habe ich diese hässlichen XML, die hat eine Menge von namespaces auf, wenn ich versuche zu laden es mit simpleXML, wenn ich an die erste namespace würd ich bekomme ein xml-Objekt ,aber nach tags mit anderen Namensräumen nicht machen würde es auf das Objekt.
Hier ist der SOAP-Antwort
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns8502="http://tempuri.org">
<SOAP-ENV:Header>
<Interface PartnerID="FastBooking" PartnerType="Test" Version="1.0.0" />
<Credentials HotelID="AC00006" User="User" Password="123456" />
<Client Ref ="123456789" TimeStamp="2012-12-14T12:24:25+0100"/>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ServiceNameRQ xmlns="urn:wsTest">
<GetRatesRS>
<Rates>
<Rate RateID="12984" FromDate="2010-05-12" ToDate="2010-06-30" RateType="PUBLIC" RateCode="RAC" MinRate="100" MaxRate="1000" Currency="EUR" ReadOnly="No" FromRateID="11456">
<RateName>Rack Rate</RateName>
</Rate>
<Rate RateID="13219" FromDate="2010-07-12" ToDate="2010-12-31" RateType="PUBLIC" RateCode="NORMAL" MinRate="100" MaxRate="1000" Currency="EUR" ReadOnly="Yes">
<RateName>Normal Rate</RateName>
<R1 Name="Single Occupancy" MinRate="90.00" MaxRate="1500.00" />
<R2 Name="Double Occupancy" MinRate="120.00" MaxRate="2000.00" />
<R3 Name="2 Nights" MinRate="150.00" MaxRate="2000.00" />
</Rate>
</Rates>
</GetRatesRS>
</ServiceNameRQ>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Hier ist der PHP code, den ich verwende:
$result = $xml_query;
$result = stripslashes($result);
$result = simplexml_load_string($result);
$namespacesMeta = $result->getNamespaces(true);
$mediaXML = $result->children($namespacesMeta['SOAP-ENV:Header']);
$Interface = $mediaXML->xpath('//Interface');
$Credentials = $mediaXML->xpath('//Credentials');
$Client = $mediaXML->xpath('//Client');
$attributesInterface = $Interface[0]['@attributes'];
$PartnerID = $attributesInterface['PartnerID'];
$PartnerType = $attributesInterface['PartnerType'];
$Version = $attributesInterface['Version'];
$attributesCredentials = $Credentials[0]['@attributes'];
$HotelID = $attributesCredentials['HotelID'];
$User = $attributesCredentials['User'];
$Password = $attributesCredentials['Password'];
$HotelID = filter_var($HotelID, FILTER_SANITIZE_MAGIC_QUOTES);
$User = filter_var($User, FILTER_SANITIZE_MAGIC_QUOTES);
$Password = filter_var($Password, FILTER_SANITIZE_MAGIC_QUOTES);
$Password = tchag3iba($Password);
Aber ich kann nicht wieder den Inhalt SOAP-ENV:Body
(GetRatesRS -> Rates -> Rate
)
- jQuery kann dies tun!
- Warum rennst du
stripslashes
auf die XML-Antwort? - mögliche Duplikate von SimpleXML SOAP-response Namespace Probleme und dies ist im Grunde Handbuch material:
SimpleXMLElement::children()
, siehe Beispiel #2 Verwendung von namespaces.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Die rate Daten zugegriffen werden kann, wie diese:
Demo
Ausgänge
Wenn Sie möchten, dass die Rate der Attribute, können Sie Sie bekommen, wie dies (innerhalb der Schleife):
Lesen Sie die
R1
undR2
Elemente wie folgt: