• 0

Help needed with caching a json file


Question

I am trying to adapt this code to help cache a json file. If I manually place the file in the cache folder the script runs fine. If I refresh the link the file is deleted and a new file is not downloaded.

 

I am hoping someone can see what I am doing wrong.

 

Thanks

laser

 

(note I tried postig this once sorry if it shows up twice)

 

I also removed my api key from the json url.

<?php

/**
* Request jobs from WU API
*
* Split the request into smaller request chunks (25 results each)
* and then consolidate them into a single array to meet the API
* requirements.
*/
function wu_api_request()
{
    file_put_contents("/cache/KHPN.json", 
file_get_contents('http://api.wunderground.com/api/xxxxxxxxxxx/geolookup/conditions/q/KHPN.json'));
}

/**
* API Request Caching
*
* Use server-side caching to store API request's as JSON at a set
* interval, rather than each pageload.
*
* @arg Argument description and usage info
*/
function json_cached_api_results( $cache_file = NULL, $expires = NULL ) {
global $request_type, $purge_cache, $limit_reached, $request_limit;

if( !$cache_file ) $cache_file = dirname(__FILE__) . '/cache/KHPN.json';
if( !$expires) $expires = time() - 2*30*30;

if( !file_exists($cache_file) ) die("Cache file is missing: 
$cache_file");

// Check that the file is older than the expire time and that it's not 
empty
if ( filectime($cache_file) < $expires || 
file_get_contents($cache_file)  == '' || $purge_cache && 
intval($_SESSION['views']) <= $request_limit ) {

  // File is too old, refresh cache
  $api_results = wu_api_request();
  $json_results = json_encode($api_results);

  // Remove cache file on error to avoid writing wrong xml
  if ( $api_results && $json_results )
   file_put_contents($cache_file, $json_results);
  else
   unlink($cache_file);
} else {
  // Check for the number of purge cache requests to avoid abuse
  if( intval($_SESSION['views']) >= $request_limit )
   $limit_reached = " <span class='error'>Request limit 
reached ($request_limit). Please try purging the cache later.</span>";
  // Fetch cache
  $json_results = file_get_contents($cache_file);
  $request_type = 'JSON';
}

return json_decode($json_results);
}

json_cached_api_results();




?>

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • No kidding, Tim, we've been running low on memory for a while now... get your head out of the Apple pixie dust cloud and come back down to earth with us peons.
    • It's really pathetic that an MMA video game triggers your political rage...
    • Nvidia GeForce NOW gains support for seven more games as discounts continue by Pulasthi Ariyasinghe There's a brand-new update rolling out to Nvidia's GeForce NOW streaming service, and like every week, that means more games have received support on the platform. This week's drop has additions like Aphelion and Pro Cycling Manager 26 attached to it. Don't forget that the GeForce NOW summer sale is still active too. This limited-time offer drops the 12-month Performance membership from $99.99 to $64.99, saving members $35. At the same time, the 12-month Ultimate membership is currently going for $129.99, dropping the price by $70 from the original $199.99. Moreover, Nvidia reiterated that support for GOG single sign-in and game library is incoming this summer, joining stores like Steam, Ubisoft Connect, Battle.net, and Xbox. "Connect supported game store accounts and stream titles with GeForce RTX power. Games that include cloud-save functionality help keep progress intact across devices," added the company. "Start a game on one screen, pick up where playtime left off on another, and spend less time managing installs and storage space." Here are the games joining GeForce NOW's supported list this week: Embers of the Uncrowned Demo (New release on Steam, available 13) Pro Cycling Manager 26 (New release on Steam, available June 15) Aphelion (Steam) Citizen Sleeper (Epic Game Store, Free from June 18-25) Megastore Simulator (Steam) OPERATOR (Steam) Super Meat Boy 3D (Xbox, available on Game Pass) Keep in mind that, unlike subscription services like Game Pass or EA Play, a copy of a game must be owned by the GeForce NOW member (or at least have a license via PC Game Pass) to start playing via Nvidia's cloud servers. There is also a limit to how many hours subscribers can use the service per month, with extra time being purchasable in chunks.
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      532
    2. 2
      +Edouard
      167
    3. 3
      PsYcHoKiLLa
      72
    4. 4
      neufuse
      64
    5. 5
      ATLien_0
      63
  • Tell a friend

    Love Neowin? Tell a friend!