Answer the question
In order to leave comments, you need to log in
How to add caching to php script?
Hello, there is a script (I have 8 of them on the site), because of it the site opens for more than 3 seconds, which was reported to me by Yandex today, I wrote to the hoster, the hoster replied that the matter was in these scripts, it was necessary to cache them.
<?php
$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;
};
$airlines = $map(json_decode(file_get_contents('http://aviaciya.com/json/airlines.json'), true), 'iata', 'name');
$cit=json_decode(file_get_contents("http://avia-avia.ru/json/cities.json")); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.travelpayouts.com/v1/prices/cheap?origin=MOW&destination=AER&limit=30&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);
$products=json_decode($response,true);
$replace_value = function($key, $val) use ($cities, $airlines){
$response = $val;
switch($key){case 'destination':
$response = $cities[$val];break;
case 'origin':
$response = $cities[$val];break;
case 'airline':
$response = $airlines[$val];
break;
}
return $response;
}
?>
<div id="kurorty" class="container-fluid">
<div class="table-responsive"><h5>Авиабилеты Москва Адлер</h5><table class="table table-hover">
<thead>
<tr>
<th style="text-align: center;"> Пункт отправления</th>
<th 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 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> <?php echo Москва ;?> </td>
<td> <?= $replace_value('destination',$key) ? $replace_value('destination', $key) : "<b>" .'Обновл.'. "</b>"; ?> </td>
<td class="mob"><img height="50" alt="" width="120" src="http://pics.avs.io/180/70/<?= $row['airline']?>.png" /></td>
<td><?php echo $replace_value('price', $row['price']); ?> RUB </td>
<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><a rel="nofollow" role="button" alt="авиабилеты Москва <?php echo $replace_value('destination',$key); ?>" title="авиабилеты Москва <?php echo $replace_value('destination',$key); ?>" href="https://bilety.avia-avia.ru/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>
</div>
$airlines = $map(json_decode(file_get_contents('http://aviaciya.com/json/airlines.json'), true), 'iata', 'name');
$cit=json_decode(file_get_contents("http://avia-avia.ru/json/cities.json")); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;}
curl_setopt($ch, CURLOPT_URL, "https://api.travelpayouts.com/v1/prices/cheap?origin=MOW&destination=AER&limit=30&token=**************");
https://ru.stackoverflow.com/questions/490089/%D0%9A%D1%8D%D1%88%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-json-%D0%BE%D1%82%D0%B2%D0%B5%D1%82%D0%B0-%D0%B8%D0%B7-instagram
) The script is extremely simple, you take json and save it, there are a couple of lines to fix your script. And in your code, only the url will need to be changed. The point is that you need to save $products to a file and read from that file."Everyone is so simple, only for me it's a rattling forest. Please help.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question