From 87f34b494b8a1a031a1d88ed36a8ebf680812a6e Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Mon, 23 May 2022 16:46:20 -0400 Subject: [PATCH] Using $_SESSION for storing and retrieving Flickr URLs Significantly decreases API calls --- scripts/overview.php | 15 ++++++++++----- scripts/todays_detections.php | 16 +++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/scripts/overview.php b/scripts/overview.php index ca308fd..39be33b 100644 --- a/scripts/overview.php +++ b/scripts/overview.php @@ -1,4 +1,7 @@ execute(); - $images = []; + if(!isset($_SESSION['images'])) { + $_SESSION['images'] = []; + } $iterations = 0; // hopefully one of the 5 most recent detections has an image that is valid, we'll use that one as the most recent detection until the newer ones get their images created while($mostrecent = $result4->fetchArray(SQLITE3_ASSOC)) { @@ -49,16 +54,16 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse if (!empty($config["FLICKR_API_KEY"])) { // if we already searched flickr for this species before, use the previous image rather than doing an unneccesary api call - $key = array_search($comname, array_column($images, 0)); + $key = array_search($comname, array_column($_SESSION['images'], 0)); if($key !== false) { - $image = $images[$key]; + $image = $_SESSION['images'][$key]; } else { $flickrjson = json_decode(file_get_contents("https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=".$config["FLICKR_API_KEY"]."&text=".str_replace("_", "%20", $comname)."&license=2%2C3%2C4%2C5%2C6%2C9&sort=relevance&per_page=5&orientation=square,portrait&format=json&media=photos&nojsoncallback=1"), true)["photos"]["photo"][0]; $modaltext = "https://flickr.com/photos/".$flickrjson["owner"]."/".$flickrjson["id"]; $authorlink = "https://flickr.com/people/".$flickrjson["owner"]; $imageurl = 'https://farm' .$flickrjson["farm"]. '.static.flickr.com/' .$flickrjson["server"]. '/' .$flickrjson["id"]. '_' .$flickrjson["secret"]. '.jpg'; - array_push($images, array($comname,$imageurl,$flickrjson["title"], $modaltext, $authorlink)); - $image = $images[count($images)-1]; + array_push($_SESSION['images'], array($comname,$imageurl,$flickrjson["title"], $modaltext, $authorlink)); + $image = $_SESSION['images'][count($_SESSION['images'])-1]; } } diff --git a/scripts/todays_detections.php b/scripts/todays_detections.php index bacd06f..0eaaf3f 100644 --- a/scripts/todays_detections.php +++ b/scripts/todays_detections.php @@ -1,4 +1,7 @@