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