2017-08-26 80 views
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>&nbsp; <?php echo Москва ;?>&nbsp;</td> 
      <td>&nbsp; <?= $replace_value('destination',$key) ? $replace_value('destination', $key) : "<b>" .'Обновл.'. "</b>"; ?>&nbsp; </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']); ?>&nbsp; RUB &nbsp; </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將需要改變。「這個想法是,您需要將$ 產品保存到文件中並從該文件讀取。 「

一切都那麼簡單,只對我來說這是一個快速森林。」請幫忙,我的手已經落下了。

非常感謝您的幫助,非常尊重。

+1

如果你不必使用這兩行,你必須保持兩個變量稱爲$ airlines et $ cities,正如Good Samaritan所說。 如果您必須在其他腳本中使用而不是在文件中,則可以將它們存儲在會話變量中。 –

+1

以這種腳本方式緩存意味着,使用遠程目標禁用file_get_contents。將文件(用php)下載到你的本地主機並從那裏讀取內容。 另外,您可以檢查filemtime以重新下載該文件,因此這將是簡單的緩存功能。 – rebru

+0

@rebru你可以修復腳本發生像你說的「簡單緩存」併發出響應嗎?我會非常感激你,我沒有寫這個網站,我準備好了,在這裏埋伏。最好的祝福 – Arcadiy

回答

1

瞧,這追加到腳本的頭

/** 
* Function to get cacheFile 
* @param $srcfile 
* @return string 
*/ 
function cacheFile($srcfile) 
{ 
    /* Defintions */ 
    /** 
    * string Cachepath is the path to your www-user writeable directory, this can be relative or absolute 
    */ 
    $cachePath = './'; 


    /** 
    * array srcfiles, key is Identifier and value is the full link to the json file 
    */ 
    $srcfiles = [ 
     'airline' => 'http://aviaciya.com/json/airlines.json', 
     'cities' => 'http://avia-avia.ru/json/cities.json' 
    ]; 


    /** 
    * Autogenerated from cachePath and srcfiles 
    */ 
    $cacheFile = $cachePath.end(explode("/", $srcfiles[$srcfile])); 


    /** 
    * int Time in seconds for cache 
    */ 
    $cachingTime = 600; 


    /** 
    * @param $cacheFile 
    * @param $url 
    */ 
    $downloader = function($cacheFile, $url) { 
     file_put_contents($cacheFile,file_get_contents($url)); 
    }; 

    /* CreateCache File if it doesnt exists */ 
    if(!file_exists($cacheFile) || time() - filemtime($cacheFile) > $cachingTime) { 
     $downloader($cacheFile, $srcfiles[$srcfile]); 
    } 

    return $cacheFile; 
} 

然後就可以調用,而不是給FQDN到的file_get_contents功能,這樣這部分代碼,...

$airlines = map(json_decode(file_get_contents(cacheFile('airline')), true), 'iata', 'name'); 
$cit=json_decode(file_get_contents(cacheFile('cities'))); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;} 

更新

  • 更好下載功能(僅求CacheFile和URL作爲參數)
  • 文件存在和filemtimecheck在一次