【发布时间】:2016-03-03 13:07:04
【问题描述】:
我将 xml 数据发送到远程服务器(网站)。得到它的回应。 现在我想将接收到的值保存在变量中以供进一步使用。但我不能这样做。
这是完整的代码:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?> <Request> <Source> <RequestorID Client="1921" EMailAddress="XML.ALTECH@TRAVELDEN.COM" Password="PASS" />
<RequestorPreferences Language="en" Currency="GBP" Country="GB">
<RequestMode>SYNCHRONOUS</RequestMode> </RequestorPreferences> </Source>
<RequestDetails> <SearchHotelPriceRequest> <ItemDestination DestinationType="city" DestinationCode="LON" />
<ImmediateConfirmationOnly /> <PeriodOfStay> <CheckInDate>2015-12-08</CheckInDate> <Duration>2</Duration>
</PeriodOfStay>
<IncludeRecommended/> <Rooms>
<Room Code="DB" NumberOfRooms="1"> <ExtraBeds> <Age>5</Age> </ExtraBeds> </Room>
<Room Code="TB" NumberOfCots="2"> <ExtraBeds> <Age>10</Age> </ExtraBeds> </Room>
<Room Code="SB" /> </Rooms> <StarRating MinimumRating="true">3</StarRating>
<OrderBy>pricelowtohigh</OrderBy> <NumberOfReturnedItems>10</NumberOfReturnedItems> </SearchHotelPriceRequest> </RequestDetails> </Request>
' ;
$url = 'https://interface.demo.gta-travel.com/wbsapi/RequestListenerServlet';
//setting the curl parameters.
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\""
);
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// send xml request to a server
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $ch;
$data = curl_exec($ch);
echo $data;
if($data === false){
$error = curl_error($ch);
echo $error;
die('error occured');
}else{
$newxml=$data;
echo $data;
echo '<pre>';
echo htmlspecialchars(print_r($newxml, true));
echo '<pre>';
$dataPOST = trim(file_get_contents($data));
$xmlData = simplexml_load_string($dataPOST);
echo $xmlData->Response->ResponseDetails->SearchHotelPriceResponse->HotelDetails->Hotel['HasMap'];
}
curl_close($ch);
}catch(Exception $e){
echo 'Message: ' .$e->getMessage();die("Error");
}
?>
我收到的错误是“尝试获取非对象的属性”,并且还有类似 Warning: file_get_contents(
$dataPOST = trim(file_get_contents($data));
$xmlData = simplexml_load_string($dataPOST);
echo $xmlData->Response->ResponseDetails->SearchHotelPriceResponse->HotelDetails->Hotel['HasMap'];
这是我在 xml 中从服务器接收的输出代码
<?xml version="1.0" encoding="UTF-8"?>
<Response ResponseReference="REF_D_010_1921-1001448826920884">
<ResponseDetails Language="en">
<SearchHotelPriceResponse>
<HotelDetails>
<Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
<City Code="LON"><![CDATA[London]]></City>
<Item Code="KEN8"><![CDATA[Grosvenor Kensington]]></Item>
<LocationDetails><Location Code="G1"><![CDATA[Central]]></Location>
<Location Code="10"><![CDATA[Kensington]]></Location></LocationDetails>
<StarRating>4</StarRating>
<HotelRooms>
<HotelRoom Code="DB" ExtraBed="true" NumberOfExtraBeds="1" NumberOfRooms="1"/>
<HotelRoom Code="TB" ExtraBed="true" NumberOfCots="2" NumberOfExtraBeds="1" NumberOfRooms="1"/>
<HotelRoom Code="SB" NumberOfRooms="1"/>
</HotelRooms>
<RoomCategories>
<RoomCategory Id="001:GRO9:5144:S5063:5758:22498">
<Description><![CDATA[Standard Triple]]></Description>
<ItemPrice CommissionPercentage="0.00" Currency="GBP">1272.00</ItemPrice>
<Confirmation Code="IM">
<![CDATA[AVAILABLE]]>
</Confirmation>
<SharingBedding>false</SharingBedding>
<Meals>
<Basis Code="N"><![CDATA[None]]></Basis></Meals>
</RoomCategory>
<RoomCategory Id="001:KEN8:5144:S5063:5705:22498">
<Description><![CDATA[Standard Triple]]></Description>
<ItemPrice CommissionPercentage="0.00" Currency="GBP">1413.00</ItemPrice>
<Confirmation Code="IM"><![CDATA[AVAILABLE]]></Confirmation>
<SharingBedding>false</SharingBedding>
<Meals><Basis Code="B"><![CDATA[Breakfast]]></Basis>
<Breakfast Code="F"><![CDATA[Full]]>
</Breakfast></Meals>
</RoomCategory>
</RoomCategories></Hotel>
</HotelDetails>
</SearchHotelPriceResponse>
</ResponseDetails>
</Response>
【问题讨论】:
-
var_dump($xmlData)是一个 simplexml 对象吗? HasMap 也是一个属性,你需要使用->attributes()->HasMap -
var_dump 不工作我试过了。
-
var_dump 不可能破解。
-
还有其他解决方案吗?我只需要将检索到的 xml 值保存在变量中
-
您可以在对象路径中不包含根 XML 元素的情况下尝试一下吗?
$xmlData->Response->ResponseDetails->变为$xmlData->ResponseDetails->。var_dump($xmlData->asXML())还有什么作用?您看到的 XML 文档与您期望的相同吗?
标签: php xml parsing xml-parsing client-server