Home>

code

<?php
$cache_ttl= 21600; //время жизни кэша в секундах
$cache_file_airlines= "tmp/airlines.data";
$cache_file_products= "tmp/products.data";
$map= function ($array, $from, $to)
{
$result= [];
if (!empty($array) &amp;
&amp;
 is_array($array))
{
foreach ($array as $element)
{
$key= $element[$from] ? : null;
$value= $element[$to] ? : null;
if ($key &amp;
&amp;
 $value)
{
$result[$key]= $value;
}
}
}
return $result;
};
if (file_exists($cache_file_airlines) &amp;
&amp;
 (time() -filemtime($cache_file_airlines)) < $cache_ttl)
{
//берём кэшированные данные
$get_airlines= file_get_contents($cache_file_airlines);
}
else
{
$get_airlines= file_get_contents('https://mysite.ru/json/airlines.json');
file_put_contents($cache_file_airlines, $get_airlines);
}
$airlines= array_column($data, 'name','iata');
if (file_exists($cache_file_products) &amp;
&amp;
 (time() -filemtime($cache_file_products)) < $cache_ttl)
{
//берём кэшированные данные
$response= file_get_contents($cache_file_products);
}
else
{
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, "https://site.ru/v3/prices_for_dates?origin=MOW&amp;
destination=AER&amp;
limit=5&amp;
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;
} ?> <div id="kurorty" class="container" style="margin-top: 20px;"><div style="margin: 0px auto;padding: 10px;text-align: center;"><h2>Билеты Москва Адлер</h2></div><div class="table-responsive"><table class="table table-hover"><thead><tr><th style="text-align: center;">Направление</th><th class="mob" style="text-align: center;"></th><th class="mob" style="text-align: center;">Дата вылета</th><th class="mob" style="text-align: center;">Обратная дата</th><th class="mob" style="text-align: center;">Цена</th></tr></thead><tbody><tr><td rowspan=6 id="Adler" style="height:356px;max-width: 370px;"><h3 class="gorod">Адлер</h3></td></tr><?php
if (isset($products['data']) &amp;
&amp;
 is_array($products['data']))
{
foreach ($products['data'] as $key=> $data)
{
foreach ($data as $destination=> $key)
{
if (preg_match('/[A-Z]{3}/i', $key))
{
?><tr><td class="mob"><img width="100" height="50" alt="авиакомпания на рейс Москва -Адлер" data-src="//pics.avs.io/100/40/<?=$data['airline'] ?>.jpg" class="lazyload air"/></td><td class="mob"><?php echo $replace_value('departure_at', substr($data['departure_at'], 0, 10)); ?></td><td class="mob"><?php echo $replace_value('return_at', substr($data['return_at'], 0, 10)); ?></td><td class="mob"> <?php echo $replace_value('price', $data['price']); ?> р. </td></tr><?php
}
}
}
}
?></tbody></table>

In this part of the code curl_setopt ($ ch, curlopt_url, "https://site.ru/v3/prices_for_dates?origin=mow& DESTINATION= AER & Limit= 5 & token= **************** ");

there is an endpoint & Limit= 5. This means in response to give the 5 first values ​​and if you go on the link https://site.ru/v3/prices_for_dates?origin=mow& DESTINATION= AER & Limit= 5 & token= **************** They will really be 5, but after processing all values ​​appear. Here is the API's answer itself when you go on the link, then you mean to processing

{"success":true,"data":[{"origin":"MOW","destination":"AER","price":1450,"airline":"5N","flight_number":"243","departure_at":"2021-09-28T05:25:00+03:00","search_id":"051a2017-af93-40df-808d-93e22d548fe4","signature":"2ea6cb4921c192001f79124cae747069","transfers":0,"return_transfers":0},{"origin":"MOW","destination":"AER","price":1462,"airline":"RL","flight_number":"707","departure_at":"2021-10-15T07:00:00+03:00","search_id":"4ed2f7da-b4c4-4eb7-8f56-80c28f761072","signature":"c38a5f7135922f5cfc6d9b9dc27bd13c","transfers":0,"return_transfers":0},{"origin":"MOW","destination":"AER","price":1462,"airline":"RL","flight_number":"707","departure_at":"2021-10-17T07:00:00+03:00","search_id":"542477cd-abab-45c6-abef-7a930006ce4a","signature":"0d812014380b23a5554889c0315b01af","transfers":0,"return_transfers":0},{"origin":"MOW","destination":"AER","price":1477,"airline":"RL","flight_number":"707","departure_at":"2021-10-14T07:00:00+03:00","search_id":"06ad509a-3c07-4523-b49e-9ab098a20bb1","signature":"59f72f531377cecd03d3bf113e3f4cae","transfers":0,"return_transfers":0},{"origin":"MOW","destination":"AER","price":1499,"airline":"N4","flight_number":"225","departure_at":"2021-11-10T16:35:00+03:00","search_id":"96daee03-3a5a-4bfd-b6d5-396f2d4fc441","signature":"69766e5c228b24327112399d7de36d25","transfers":0,"return_transfers":0}],"currency":"rub"}

Question Where is the error? Thank you

There are 5 values. what's the catch?

Алексей Шиманский2021-07-12 00:26:08

@ Alekseyshimansky if the link I see them 5, and after processing IPA I do not work, this endoint is shown, more than five

Arcadiy2021-07-12 00:26:08

@ Alekseyshimansky before processing API, after my processing, there are much more of them, then you mean somewhere the error in my code.

Arcadiy2021-07-12 00:26:08

"But after processing" ---after what processing?

Алексей Шиманский2021-07-12 00:26:08

@ Alekseyshimansky you are here?

Arcadiy2021-07-11 12:06:08