0
有一个脚本(我有这样的现场8)如何添加缓存到php脚本?
<?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;}
这就是一个很好的人写信给我:
“脚本非常简单,你把json保存下来,有几行 用于纠正你的脚本,并且在你的代码中只有 U RL将需要改变。“这个想法是,您需要将$ 产品保存到文件中并从该文件读取。 “
一切都那么简单,只对我来说这是一个快速森林。”请帮忙,我的手已经落下了。
非常感谢您的帮助,非常尊重。
如果你不必使用这两行,你必须保持两个变量称为$ airlines et $ cities,正如Good Samaritan所说。 如果您必须在其他脚本中使用而不是在文件中,则可以将它们存储在会话变量中。 –
以这种脚本方式缓存意味着,使用远程目标禁用file_get_contents。将文件(用php)下载到你的本地主机并从那里读取内容。 另外,您可以检查filemtime以重新下载该文件,因此这将是简单的缓存功能。 – rebru
@rebru你可以修复脚本发生像你说的“简单缓存”并发出响应吗?我会非常感激你,我没有写这个网站,我准备好了,在这里埋伏。最好的祝福 – Arcadiy