我有如下所示的SOAP XML响应,请建议我如何从中找到房间代码,酒店代码,货币等数据。 我也使用下面的代码,但它不工作意味着它返回空。
$xmlObject = simplexml_load_string($xmlString);
$array = json_decode(json_encode($xmlObject), true);
相应的XML响应:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ResponseId xmlns="http://www.example.com/">e96c9439-6049-4abb-b070-f7f0024153b7@52-20-187-176</ResponseId>
</soap:Header>
<soap:Body>
<ns4:FetchRoomAllotmentResponse xmlns:ns4="http://www.example.com/" xmlns:ns2="http://www.example.com" xmlns:ns3="http://www.example.com/">
<ns4:FetchRoomAllotmentResult>
<ns4:Response>
<ns4:Hotel Code="201511191642109768" Name="TestCMHotel" />
<ns4:Rooms>
<ns4:Room Code="171578" Name="2 Bed Super Deluxe">
<ns4:Rates>
<ns4:Rate Currency="INR" Description="2 Bed Super Deluxe" Id="71624608833328" MealPlan="MAP" ValidFrom="2015-11-19" ValidTo="2017-04-30">
<ns4:Inclusions>
<ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
</ns4:Inclusions>
</ns4:Rate>
</ns4:Rates>
</ns4:Room>
<ns4:Room Code="19505" Name="AC Deluxe Room AC">
<ns4:Rates>
<ns4:Rate Currency="INR" Description="AC Deluxe Room AC" Id="5835231260301" MealPlan="EP" ValidFrom="2015-11-19" ValidTo="2017-05-31">
<ns4:Inclusions>
<ns4:Inclusion code="test 1">test 1</ns4:Inclusion>
<ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
</ns4:Inclusions>
</ns4:Rate>
</ns4:Rates>
</ns4:Room>
<ns4:Room Code="18303" Name="Standar AC">
<ns4:Rates>
<ns4:Rate Currency="INR" Description="Standar AC" Id="79045408874412" MealPlan="CP" ValidFrom="2015-11-19" ValidTo="2017-06-30">
<ns4:Inclusions>
<ns4:Inclusion code="test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te">test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te</ns4:Inclusion>
</ns4:Inclusions>
</ns4:Rate>
</ns4:Rates>
</ns4:Room>
</ns4:Rooms>
</ns4:Response>
</ns4:FetchRoomAllotmentResult>
</ns4:FetchRoomAllotmentResponse>
</soap:Body>
</soap:Envelope>
答案 0 :(得分:1)
尝试此示例soap响应数组值get:
if( ! $response = curl_exec($cURL)){<br/>
trigger_error(curl_error($cURL));<br/>
}
curl_close ( $cURL );<br/><br/>
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);<br/>
$xml = simplexml_load_string($xml);<br/>
$json = json_encode($xml);<br/>
$responseArray = json_decode($json,true);
//Find value
findKey($responseArray['sBody'],'val');
此功能有助于查找关键字&amp;数组中的值
public static function findKey($array, $keySearch)
{
foreach ($array as $key => $item) {
if ($key === $keySearch) {
return $item;
}
else {
if (is_array($item)) {
return self::findKey($item, $keySearch);
}
}
}
return false;
}
答案 1 :(得分:0)
使用simplexml_load_string()
:
将XML字符串解释为对象
此处有更多信息 http://php.net/manual/en/function.simplexml-load-string.php
答案 2 :(得分:0)
可能这对你有用。
$xml = new SimpleXMLElement($xmlObject);
$xml->registerXPathNamespace('ns4', 'http://www.example.com/');
foreach($xml->xpath('//ns4:Room') as $room) {
//var_export($room);
//var_export($room->xpath("//ns4:Inclusion"));
echo "ROOM Code" . $room["Code"]."<br/>";
foreach($room->xpath("//ns4:Inclusion") as $inclusion){
echo "Inclusion Code" .$inclusion["code"]."<br/>";
}
}
了解更多相关信息
答案 3 :(得分:0)
$parser = simplexml_load_string($result);
$parserEnv = $parser->children('soap', true);
$hotel = $parserEnv->Body->children('ns4', true)
->FetchRoomAllotmentResponse->children('ns4', true)
->FetchRoomAllotmentResult->children('ns4', true)
->Response->children('ns4', true)->Hotel[0]->attributes();
echo 'Hotel Code= '.$hotel['Code']. '<br>';
echo 'Hotel Name= '.$hotel['Name']. '<br>';
$room = $parserEnv->Body->children('ns4', true)
->FetchRoomAllotmentResponse->children('ns4', true)
->FetchRoomAllotmentResult->children('ns4', true)
->Response->children('ns4', true)->Rooms->children('ns4', true);
for($i=0; $i<count($room); $i++)
{
$r=$room->Room[$i]->attributes();
echo 'Room'.$i.' Code='.$r['Code']. '<br>';
echo 'Room'.$i.' Name='.$r['Name']. '<br>';
$rate=$room->Room[$i]->children('ns4', true)->Rates->children('ns4', true)->Rate[0]->attributes();
echo 'Currency= '.$rate['Currency']. '<br>';
echo 'Description= '.$rate['Description']. '<br>';
echo 'Id= '.$rate['Id']. '<br>';
echo 'MealPlan= '.$rate['MealPlan']. '<br>';
echo 'ValidFrom= '.$rate['ValidFrom']. '<br>';
echo 'ValidTo= '.$rate['ValidTo']. '<br>';