This topic is locked

Caching API Results

5/7/2020 11:13:24 AM
PHPRunner General questions
W
WisTex author

Is there a way to cache API results, perhaps to the database?
Some APIs don't change much or are updated on set schedules (daily, for example). Constantly pinging the API for duplicate requests adds strain on their server, and for some APIs, it may increase your cost if they bill for API usage.
It would be nice if there was a way to cache the API results for a set period, and even nicer if this cache could be stored in the database.

Sergey Kornilov admin 5/7/2020

We plan to add such a built-in feature/option.

fhumanes 5/9/2020



Is there a way to cache API results, perhaps to the database?
Some APIs don't change much or are updated on set schedules (daily, for example). Constantly pinging the API for duplicate requests adds strain on their server, and for some APIs, it may increase your cost if they bill for API usage.
It would be nice if there was a way to cache the API results for a set period, and even nicer if this cache could be stored in the database.


Hello:
While that option is implemented in a new version, you can use this form, explained in this example, to cache any information from an API.
https://asprunner.com/forums/topic/27340-phprunner-app-client-restful-api/
Cheers,

fernando

W
WisTex author 5/10/2020



Hello:
While that option is implemented in a new version, you can use this form, explained in this example, to cache any information from an API.
https://asprunner.com/forums/topic/27340-phprunner-app-client-restful-api/
Cheers,

fernando



Thanks for the link. It provides a nice walkthrough on setting up an API.
But I don't see where it caches the data to my database.
The developer said that was not possible yet, but coming soon.

fhumanes 5/10/2020

Hello:
In the link an application / example is delivered where you work with the PHPRunner API and elsewhere, more or less the same is done, but in this case, retrieving the data and loading it into the Database, showing the data from the database.
An example of the code:





<?php

@ini_set("display_errors","1");

@ini_set("display_startup_errors","1");

require_once __DIR__ . '/../../ComponentCode/unirest_3.0.4/autoload.php';

function valueToZero($val){

return empty($val)?0:$val;

}

$session_name = session_id();

// Delete the covid_statistics of session

$sql="delete FROM covid_statistics where SessionName = '$session_name'";

$resql=db_query($sql,$conn);

$response = Unirest\Request::get("https://covid-193.p.rapidapi.com/statistics";,

array(

"X-RapidAPI-Host" => "covid-193.p.rapidapi.com",

"X-RapidAPI-Key" => "4bc3052c61msh7345bc8ec10474ap10192ejsn016e636a3859"

)

);

$a = $response->code; // HTTP Status code

$b = $response->headers; // Headers

$c = $response->body; // Parsed body

$d = $response->raw_body; // Unparsed body

$f = json_decode($d,true);

// var_dump($f); // DUMP pf array

$count = count($f["response"]);

foreach($f["response"] as $response) {

// $SessionName,

$country = $response['country'];

$day = $response['day'];

$CasesNew = $response['cases']['new'];

$CasesActive = $response['cases']['active'];

$CasesCritical = $response['cases']['critical'];

$CasesRecovered = $response['cases']['recovered'];

$CasesTotal = $response['cases']['total'];

$DeathsNew = $response['deaths']['new'];

$DeathsTotal = $response['deaths']['total'];

$TestTotal = $response['tests']['total'];



$CasesNew = valueToZero($CasesNew); // if Null = 0

$CasesActive = valueToZero($CasesActive); // if Null = 0

$CasesCritical = valueToZero($CasesCritical); // if Null = 0

$CasesRecovered = valueToZero($CasesRecovered); // if Null = 0

$CasesTotal = valueToZero($CasesTotal); // if Null = 0

$DeathsNew = valueToZero($DeathsNew); // if Null = 0

$DeathsTotal = valueToZero($DeathsTotal); // if Null = 0

$TestTotal = valueToZero($TestTotal); // if Null = 0

$sql="insert INTO covid_statistics

(SessionName,country,dataDay,CasesNew,CasesActive,CasesCritical,CasesRecovered,CasesTotal,DeathsNew,DeathsTotal,TestTotal)

VALUES ('$session_name','$country','$day',$CasesNew,$CasesActive,$CasesCritical,$CasesRecovered,$CasesTotal,$DeathsNew,$DeathsTotal,$TestTotal)";

$resql=db_query($sql,$conn);



}


Cheers,