Отправить XML-данные на сервер, получить их и сохранить в переменные в переполнении стека

Я отправляю данные 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>

1

Решение

var_dump($xmlData) будет ложным с вашим кодом …

С помощью file_get_contents() в недопустимом вы пытаетесь «загрузить с диска», когда $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;
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector