我将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");
}
?>
我收到的错误是“尝试获取非对象的属性”,并且还警告警告: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>
答案 0 :(得分:0)
var_dump($xmlData)
将是&#34; false&#34;用你的代码......
使用file_get_contents()
无效,您正试图从磁盘加载&#34;当$data
是一个字符串时。
您还试图错误地获取属性。
找到此代码:
$dataPOST = trim(file_get_contents($data));
$xmlData = simplexml_load_string($dataPOST);
echo $xmlData->Response->ResponseDetails->SearchHotelPriceResponse->HotelDetails->Hotel['HasMap'];
将其更改为此代码:
$xmlData = simplexml_load_string($data);
echo $xmlData->ResponseDetails->SearchHotelPriceResponse->HotelDetails->Hotel->attributes()->HasMap;