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();
?>
Neowin says it's $39.99, but their site says $399.99 after discount. Is Neowin covering the difference? I'll take 7 if they are. Not 8, not 6, never 5, but 7.
Yeah and now they added space above the tab bar, so now I have to painstakingly aim at just slightly below the top of the screen to reach a tab with my mouse.
truly hate it
Question
Laser
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(); ?>Link to comment
https://www.neowin.net/forum/topic/1159552-help-needed-with-caching-a-json-file/Share on other sites
1 answer to this question
Recommended Posts