gpt4 book ai didi

php - 使用 PHP 解析带有命名空间的 SOAP XML 响应

转载 作者:行者123 更新时间:2023-12-04 16:51:05 25 4
gpt4 key购买 nike

我有这个丑陋的 XML,它上面有很多命名空间,当我尝试用 simpleXML 加载它时,如果我指出第一个命名空间,我会得到一个 xml 对象,但是跟随带有其他命名空间的标签不会进入该对象。
这是 SOAP 响应

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

这是我正在使用的 PHP 代码:
$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);

但我无法检索 SOAP-ENV:Body ( GetRatesRS -> Rates -> Rate ) 的内容

最佳答案

速率数据可以这样访问:

Demo

$obj = simplexml_load_string($xml);

foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
{
echo (string)$rate->RateName . "\n";
}

输出

Rack Rate
Normal Rate



如果您想要 Rate 属性,您可以像这样(在循环内)获取它们:
echo $rate->attributes()->RateID;

您可以阅读 R1R2像这样的元素:
foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
{
if(isset($rate->R1))
{
echo $rate->R1->attributes()->Name;
echo $rate->R1->attributes()->MinRate;
}
if(isset($rate->R2))
{
echo $rate->R2->attributes()->Name;
echo $rate->R2->attributes()->MinRate;
}
}

关于php - 使用 PHP 解析带有命名空间的 SOAP XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13929566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com