Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. <?php
  2.  
  3. $map = function($array, $from, $to){
  4. $result = [];
  5. if(!empty($array) && is_array($array)){
  6. foreach ($array as $element) {
  7. $key = $element[$from]?:null;
  8. $value = $element[$to]?:null;
  9. if($key && $value){
  10. $result[$key] = $value;
  11. }
  12. }
  13. }
  14. return $result;
  15. };
  16.  
  17. $airlines = $map(json_decode(file_get_contents('http://site.com/json/airlines.json'), true), 'iata', 'name');
  18.  
  19. $cit=json_decode(file_get_contents("http://site.com/json/cities.json")); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;}
  20.  
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v2/prices/latest?currency=rub&period_type=year&page=1&limit=30&token=token&show_to_affiliates=true&currency=rub&sorting=route");
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  24. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  25. curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: token"));
  26. $response = curl_exec($ch);
  27. curl_close($ch);
  28.  
  29. $products=json_decode($response,true);
  30.  
  31.  
  32.  
  33.  
  34. $replace_value = function($key, $val) use ($cities, $airlines){
  35. $response = $val;
  36. switch($key){case 'destination':
  37. $response = $cities[$val];break;
  38. case 'origin':
  39. $response = $cities[$val];break;
  40. case 'airline':
  41. $response = $airlines[$val];
  42. break;
  43. }
  44. return $response;
  45. }
  46.  
  47. ?>
  48. <table class="mytable">
  49. <thead style="background: rgba(2, 69, 141, 0.46) none repeat scroll 0% 0%; color: rgb(255, 255, 255);">
  50. <tr>
  51. <th class="views-field views-field-field-logo-fid">
  52. Кол-во </th>
  53. <th class="views-field views-field-tid">
  54. Класс </th>
  55. <th class="views-field views-field-tid-1">
  56. Вылет </th>
  57. <th class="views-field views-field-field-napravlenie-value">Прилёт
  58. </th>
  59. <th class="views-field views-field-field-cenaot-value">
  60. Дата В </th>
  61. <th class="views-field views-field-field-prodsgas-value">Обратно</th>
  62. <th class="views-field views-field-field-prodsgapo-value">
  63. Пересадок </th>
  64. <th class="views-field views-field-field-perelets-value">Цена руб.</th>
  65. <th class="views-field views-field-field-pereletpo-value">
  66. Дата поиска</th>
  67. <th class="views-field views-field-nothing">
  68. Расстояние (км.) </th><th class="views-field views-field-Actual">
  69. Актуально</th></tr>
  70. </thead>
  71. <?php if(isset($products['data']) && is_array($products['data'])){?>
  72. <?php foreach ($products['data'] as $key => $data) {?>
  73.  
  74.  
  75.  
  76.  
  77. <?php foreach ($data as $field=> $val) {?>
  78.  
  79.  
  80. <td><?php echo $replace_value($field, $val);?></td> <?}?>
  81. </tr>
  82. <?php }?> <?}?>
  83. </table>
  84.  
  85. {"success":true,"data":{"AER":{"origin":"MOW","destination":"AER","price":3300,"transfers":0,"airline":"S7","flight_number":1021,"departure_at":"2016-03-12T23:30:00Z","return_at":"2016-03-16T16:40:00Z","expires_at":"2016-02-09T13:11:16Z"},"BCN":{"origin":"MOW","destination":"BCN","price":12749,"transfers":0,"airline":"U6","flight_number":7045,"departure_at":"2016-06-04T04:55:00Z","return_at":"2016-06-11T09:30:00Z","expires_at":"2016-02-09T14:21:23Z"},"BKK":{"origin":"MOW","destination":"BKK","price":27016,"transfers":1,"airline":"GF","flight_number":15,"departure_at":"2016-02-23T14:50:00Z","return_at":"2016-05-02T21:30:00Z","expires_at":"2016-02-09T12:43:02Z"},"HKT":{"origin":"MOW","destination":"HKT","price":32971,"transfers":1,"airline":"CA","flight_number":910,"departure_at":"2016-02-25T18:55:00Z","return_at":"2016-03-17T01:40:00Z","expires_at":"2016-02-07T09:28:18Z"},"KGD":{"origin":"MOW","destination":"KGD","price":3500,"transfers":0,"airline":"S7","flight_number":67,"departure_at":"2016-02-10T14:00:00Z","return_at":"2016-03-17T16:00:00Z","expires_at":"2016-02-07T20:42:05Z"},"KRR":{"origin":"MOW","destination":"KRR","price":3465,"transfers":0,"airline":"DP","flight_number":157,"departure_at":"2016-03-01T19:05:00Z","return_at":"2016-03-07T13:35:00Z","expires_at":"2016-02-09T13:13:08Z"},"LED":{"origin":"MOW","destination":"LED","price":2789,"transfers":0,"airline":"U6","flight_number":91,"departure_at":"2016-03-15T11:50:00Z","return_at":"2016-03-16T16:00:00Z","expires_at":"2016-02-06T21:40:46Z"},"MRV":{"origin":"MOW","destination":"MRV","price":3271,"transfers":0,"airline":"S7","flight_number":1215,"departure_at":"2016-03-17T16:40:00Z","return_at":"2016-03-23T19:55:00Z","expires_at":"2016-02-06T18:57:52Z"},"SIP":{"origin":"MOW","destination":"SIP","price":3965,"transfers":0,"airline":"WZ","flight_number":301,"departure_at":"2016-02-21T17:30:00Z","return_at":"2016-02-25T12:30:00Z","expires_at":"2016-02-09T12:21:59Z"},"TBS":{"origin":"MOW","destination":"TBS","price":8071,"transfers":0,"airline":"S7","flight_number":955,"departure_at":"2016-03-08T12:50:00Z","return_at":"2016-03-11T17:25:00Z","expires_at":"2016-02-09T12:57:16Z"}},"error":null,"currency":"rub"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement