I am trying to figure out how I would go about caching the data I am pulling from a webservce json file onto my page so that I do not continually request this data and bring down the server.
I currently am pullin the json data like so:
// jSON URL which should be requested
$json_url = 'http://example.com/datastore.json?toolbar_id='.$persona['toolbar_id'].'';
// jSON String for request
$json_string = '[Json string? What is this]';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting jSON result string
$result = json_decode($result, true);
$result = $result[0];
From here I can pull the associative array results as I need them. But If I were to refresh the page, it would recall the server info. Any solutions?
You’d treat it like any other cache file:
filemtime()against the currenttime()cURLcall and write the data to the cache file and carry onIt will be the same JSON regardless if PHP returns it via
cURLor if PHP returns it viafread()on a cache file.