@sochi-russia

Не могу вывести данные из АПИ, как исправить?

Есть код

<?php
$cache_ttl = 900; // время жизни кэша в секундах
$cache_file_airlines = "http://*******/tmp/airlines.data";
$cache_file_products = "http://*******/tmp/products.data";


$map = function ($array, $from, $to)
{
    $result = [];
    if (!empty($array) && is_array($array)) {
        foreach ($array as $element) {
            $key = $element[$from] ? : null;
            $value = $element[$to] ? : null;
            if ($key && $value) {
                $result[$key] = $value;
            }
        }
    }
    return $result;
};

if (file_exists($cache_file_airlines) && (time() - filemtime($cache_file_airlines)) < $cache_ttl) {
    // берём кэшированные данные
    $get_airlines = file_get_contents($cache_file_airlines);
} else {
    $get_airlines = file_get_contents('http://*********/json/airlines.json');
    file_put_contents($cache_file_airlines, $get_airlines);
}
$airlines = $map(json_decode($get_airlines, true), 'iata', 'name');



if (file_exists($cache_file_products) && (time() - filemtime($cache_file_products)) < $cache_ttl) {
    // берём кэшированные данные
    $response = file_get_contents($cache_file_products);
} else {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/prices/calendar?currency=RUB&origin=MOW&destination=AER&token=***********");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: *******"));
    $response = curl_exec($ch);
    curl_close($ch);

    file_put_contents($cache_file_products, $response);
}

$products = json_decode($response, true);
$replace_value = function ($key, $val)use ($airlines)
{
    $response = $val;
    switch ($key) {
            case 'airline':
            $response = $airlines[$val];
            break;
    }
    return $response;
}?>









 <table id="example" class="display">
    <thead>
      <tr>
       <th class="mob" style="text-align: center;">Дата вылета</th>
<th class="mob" style="text-align: center;">Обратная дата</th>
 <th style="text-align: center;">Цена, р.</th> 
        <th class="mob" style="text-align: center;"> Авиакомпания</th>
<th> </th>
      </tr>
    </thead>    <tbody>
<?php
if(isset($products['data']) && is_array($products['data'])) {
foreach ($products['data'] as $key => $data) {
foreach ($data as $destination => $row) {
if (preg_match('/[A-Z]{3}/i', $key)) {
?>
<tr><td class="mob"><?php echo $replace_value('departure_at', substr($row['departure_at'], 0, 10)); ?></td>
<td class="mob"><?php echo $replace_value('return_at', substr($row['return_at'], 0, 10)); ?></td>
<td><?php echo $replace_value('price', $row['price']); ?>&nbsp;  RUB &nbsp; </td>
 <td class="mob"><img height="50" alt="" width="120" src="http://pics.avs.io/180/70/<?= $row['airline']?>.png" /></td>


      <td><a rel="nofollow" role="button" alt="авиабилеты Москва <?php echo $replace_value('destination',$key); ?>" title="Москва <?php echo $replace_value('destination',$key); ?>" href="https://*****.com/flights/?origin_iata=MOW&destination_iata=<?=$key?>&depart_date=<?=substr($row['departure_at'], 0, 10)?>&return_date=<?=substr($row['return_at'], 0, 10)?>&adults=1&children=0&infants=0&trip_class=0&marker=87111&with_request=true">Поиск</a></td></tr>
<?php
}
}
}
}
?>  </tbody>
  </table>


Есть ответ API
{"success":true,"data":{"2017-11-25":{"origin":"MOW","destination":"AER","price":3750,"transfers":2,"airline":"SU","flight_number":1394,"departure_at":"2017-11-25T23:15:00Z","return_at":"2017-12-28T03:45:00Z","expires_at":"2017-11-25T23:15:00Z"},"2017-11-26":{"origin":"MOW","destination":"AER","price":2343,"transfers":0,"airline":"DP","flight_number":113,"departure_at":"2017-11-26T20:00:00Z","return_at":"2017-12-07T07:25:00Z","expires_at":"2017-11-26T20:00:00Z"},"2017-11-27":{"origin":"MOW","destination":"AER","price":2343,"transfers":0,"airline":"DP","flight_number":111,"departure_at":"2017-11-27T07:50:00Z","return_at":"2017-12-06T07:25:00Z","expires_at":"2017-11-26T08:31:52Z"},"2017-11-28":{"origin":"MOW","destination":"AER","price":2343,"transfers":0,"airline":"DP","flight_number":113,"departure_at":"2017-11-28T20:00:00Z","return_at":"2017-12-28T08:00:00Z","expires_at":"2017-11-27T12:02:15Z"},"2017-11-29":{"origin":"MOW","destination":"AER","price":2343,"transfers":0,"airline":"DP","flight_number":113,"departure_at":"2017-11-29T20:00:00Z","return_at":"2017-12-13T07:25:00Z","expires_at":"2017-11-28T12:46:25Z"},"2017-11-30":{"origin":"MOW","destination":"AER","price":5437,"transfers":0,"airline":"U6","flight_number":449,"departure_at":"2018-11-20T15:45:00Z","return_at":"2018-11-22T19:00:00Z","expires_at":"2017-11-28T15:33:59Z"}},"error":null,"currency":"RUB"}


Данные в таблице не появляются((

Документация по АПИ
http://api.travelpayouts.com/?api_token=&locale=en&code=php#v1_prices_calendar_endpoint

https://support.travelpayouts.com/hc/ru/articles/203956163#07
  • Вопрос задан
  • 352 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы