[英]Merging API JSON response with XML using PHP
我在这里有一个棘手的问题,我正在寻找一些建议来帮助我获得所需的结果。
因此,我正在使用一个用于酒店预订引擎的API,该API不错,但是它缺少客户端在单独的静态XML文件中提供的一些内容(例如,房间,设施等的描述)。
因此,当用户搜索可用性时,我需要做4件事。
从API获取所有可用房型的列表。 下方的JSON:
{“结果”:[{“ id”:147,“名称”:“ XXXXXXX Loft XL”,“ description”:“ XXXXXXX Loft XL”,“ code”:“ B1K”,“ max_occupancy”:2,“ level” :2},{“ id”:145,“ name”:“ XXXXXXX Loft”,“ description”:“ XXXXXXX Loft”,“ code”:“ C1K”,“ max_occupancy”:2,“ level”:1}] ,“ total_count”:2}
获取每个房间的可用性,并在房间类型内显示每个房间的4个不同价格。
合并包含房间说明,图标,设施等的本地静态XML文件以及房间类型API响应。
这是要数组的XML:
$roomdata = simplexml_load_file('room-data.xml');
// put this into an array of sorts
$roomjson = json_encode($roomdata);
$roominformation = json_decode($roomjson,TRUE);
我曾尝试将事物放入数组并匹配键(在这种情况下为id),但我并不高兴,只是将它们分开。
关于实现此目标的最佳方法有什么想法?
以下是到目前为止我正在使用API并尝试将其与XML结合使用的一些代码片段。
$url = 'https://www.myapi.com'; // this gets the room types
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
// Decode the response
$responseData = json_decode($result, TRUE);
// Put everyting to the screen with var_dump;
//var_dump($responseData);
// With print_r ( useful for arrays );
//print_r($responseData);
// List just review ratings with foreach;
$results = $responseData['results'];
// create an class for rooms
$room_types = new stdClass;
$index = 1;
foreach ($results as $rooms) {
// This appends a new element to $room_types, in this case the value is another array
$room_types_array[] = array('id' => $rooms['id'] ,'name' => $rooms['name'],'description' => $rooms['description']);
$room_types = (object) $room_types_array;
}
// merge the arrays
$room_data_merged = array_merge_recursive($room_types_array, $roominformation);
当我尝试将XML和API组合为房间类型时,这是数组响应:
Array
(
[0] => Array
(
[id] => 147
[name] => XXXXXXX Loft XL
[description] => XXXXXXX Loft XL
)
[1] => Array
(
[id] => 145
[name] => XXXXXXX Loft
[description] => XXXXXXX Loft
)
[room] => Array
(
[0] => Array
(
[id] => 145
[roomname] => XXXXXXX Loft
[intro] => The 24m² XXXXXXX Loft sets new standards in the intelligent use of space. As a living/working hybrid, it can be easily adjusted to become cozy, business-like or something in between.
[introicons] => Array
(
[iconitem] => Array
(
[0] => Array
(
[icon] => one-to-two-people
[icondescription] => 1–2 Adults
)
[1] => Array
(
[icon] => wi-fi
[icondescription] => Wi-Fi
)
[2] => Array
(
[icon] => whisk
[icondescription] => Kitchen
)
[3] => Array
(
[icon] => bed
[icondescription] => 1 King Size Bed
)
)
)
[information] => Array
(
[informationitem] => Array
(
[0] => Array
(
[icon] => bed
[title] => King-Size Bed
[description] => Our lovely beds guarantee the sweetest of dreams thanks to the layered mattress, fluffy duvet and 100% organic bed linen.
)
[1] => Array
(
[icon] => whisk
[title] => Fully Equipped Kitchen
[description] => The kitchen comes with built-in Siemens luxury equipment and is capable of pumping out the perfect cappuccinos in the morning and the tastiest of pastas at night.
)
[2] => Array
(
[icon] => table
[title] => Large Kitchen Table
[description] => At XXXXXXX, a big kitchen table serves as focal point. Use it to work across time zones, host dinner parties or gently rest your head after making deadline.
)
[3] => Array
(
[icon] => shower
[title] => Spacious Shower Area
[description] => The shower area features an awesome high-pressure shower and amenities.
)
[4] => Array
(
[icon] => wi-fi
[title] => Wi-Fi
[description] => Free, fast and reliable internet (wired & wireless) - EVERYWHERE.
)
[5] => Array
(
[icon] => coffee
[title] => Living Room
[description] => Lounge-styled living area with Muuto design couch and side table, along with a 32-Inch smart LED TV including BYOC (Bring Your Own Content) option. Complete with prickly-but-cosy green cacti and swappable art.
)
)
)
)
[1] => Array
(
[id] => 147
[roomname] => XXXXXXX Loft XL
[intro] => The extra spacious XXXXXXX Loft of 32m² sets new standards in the intelligent use of space. As a living/working hybrid, it can be easily adjusted to become cozy, business-like or something in between.
[introicons] => Array
(
[iconitem] => Array
(
[0] => Array
(
[icon] => one-to-two-people
[icondescription] => 1–2 Adults
)
[1] => Array
(
[icon] => wi-fi
[icondescription] => Wi-Fi
)
[2] => Array
(
[icon] => whisk
[icondescription] => Kitchen
)
[3] => Array
(
[icon] => bed
[icondescription] => 1 King Size Bed
)
)
)
[information] => Array
(
[informationitem] => Array
(
[0] => Array
(
[icon] => bed
[title] => King-Size Bed
[description] => Our lovely beds guarantee the sweetest of dreams thanks to the layered mattress, fluffy duvet and 100% organic bed linen.
)
[1] => Array
(
[icon] => whisk
[title] => Fully Equipped Kitchen
[description] => The kitchen comes with built-in Siemens luxury equipment and is capable of pumping out the perfect cappuccinos in the morning and the tastiest of pastas at night.
)
[2] => Array
(
[icon] => table
[title] => Large Kitchen Table
[description] => At XXXXXXX, a big kitchen table serves as focal point. Use it to work across time zones, host dinner parties or gently rest your head after making deadline.
)
[3] => Array
(
[icon] => shower
[title] => Spacious Shower Area
[description] => The shower area features an awesome high-pressure shower and amenities.
)
[4] => Array
(
[icon] => wi-fi
[title] => Wi-Fi
[description] => Free, fast and reliable internet (wired & wireless) - EVERYWHERE.
)
[5] => Array
(
[icon] => coffee
[title] => Living Room
[description] => Lounge-styled living area with Muuto design couch and side table, along with a 32-Inch smart LED TV including BYOC (Bring Your Own Content) option. Complete with prickly-but-cosy green cacti and swappable art.
)
)
)
)
[2] => Array
(
[id] => 146
[roomname] => XXXXXXX Loft – Wall View
[intro] => A budget smart twist on our 24m² XXXXXXX Loft: All the same smart features, minus a view.
[introicons] => Array
(
[iconitem] => Array
(
[0] => Array
(
[icon] => obstructed-view
[icondescription] => Obstructed View
)
[1] => Array
(
[icon] => one-to-two-people
[icondescription] => 1–2 Adults
)
[2] => Array
(
[icon] => wi-fi
[icondescription] => Wi-Fi
)
[3] => Array
(
[icon] => whisk
[icondescription] => Kitchen
)
[4] => Array
(
[icon] => bed
[icondescription] => 1 King Size Bed
)
)
)
[information] => Array
(
[informationitem] => Array
(
[0] => Array
(
[icon] => bed
[title] => King-Size Bed
[description] => Our lovely beds guarantee the sweetest of dreams thanks to the layered mattress, fluffy duvet and 100% organic bed linen.
)
[1] => Array
(
[icon] => whisk
[title] => Fully Equipped Kitchen
[description] => The kitchen comes with built-in Siemens luxury equipment and is capable of pumping out the perfect cappuccinos in the morning and the tastiest of pastas at night.
)
[2] => Array
(
[icon] => table
[title] => Large Kitchen Table
[description] => At XXXXXXX, a big kitchen table serves as focal point. Use it to work across time zones, host dinner parties or gently rest your head after making deadline.
)
[3] => Array
(
[icon] => shower
[title] => Spacious Shower Area
[description] => The shower area features an awesome high-pressure shower and amenities.
)
[4] => Array
(
[icon] => wi-fi
[title] => Wi-Fi
[description] => Free, fast and reliable internet (wired & wireless) - EVERYWHERE.
)
[5] => Array
(
[icon] => coffee
[title] => Living Room
[description] => Lounge-styled living area with Muuto design couch and side table, along with a 32-Inch smart LED TV including BYOC (Bring Your Own Content) option. Complete with prickly-but-cosy green cacti and swappable art.
)
)
)
)
)
)
一旦将所有房间数据汇总在一起,就必须以某种方式在每个DIV中循环可用的费率。
任何帮助将不胜感激!
西蒙
您只需要根据一些唯一键搜索各种数组(我猜示例代码中的id
。
foreach ($results as $rooms) {
// First index using your unique key
$result[$rooms['id']]['room'] = $rooms
// Build a couple of search functions to grab an array based on unique key
$result[$rooms['id']]['room_info']=search_your_json($json,$rooms['id']);
$result[$rooms['id']]['room_data']=search_other_data_function($rooms['id']);
}
然后,您将得到一个类似的数组:
[
145=>[
'room'=>[//array of room],
'room_info'=>[//array of room info],
'room_data'=>[//array of room data]
],
147=>[
//same array structure here
]
]
我想我对各种数据源都有误解,但您应该明白这一点。
示例搜索功能
search_your_json($json,$room_id){
$data = json_decode($json,true);//true for an array
foreach($data as $d){
if($d['id']===$room_id){//if it has the right id return the array $d
return $d
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.