This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
BiblioMxWeb/server/isbnImage.php

40 lines
1.2 KiB
PHP
Raw Normal View History

2021-11-08 18:29:45 +01:00
<?php
function cache_get($isbn){
2021-11-09 22:22:52 +01:00
if(!file_exists("isbnCacheImg.json")) return false;
$data = json_decode(file_get_contents("isbnCacheImg.json"),true);
2021-11-08 18:29:45 +01:00
if(!$data) return false;
$res = $data[$isbn];
if(!$res) return false;
if($res["time"] + $GLOBALS["imageISBNCache"] < time()) return false;
return $res["url"];
}
function cache_set($isbn,$url){
if(file_exists("2/isbnCacheImg.json")){
2021-11-09 22:22:52 +01:00
$data = json_decode(file_get_contents("isbnCacheImg.json"),true)?:[];
2021-11-08 18:29:45 +01:00
}
else{
$data = [];
}
$data[$isbn] = ["time"=>time(),"url"=>$url];
file_put_contents("2/isbnCacheImg.json", json_encode($data));
}
function isbnImage($isbn){
$url = cache_get($isbn);
if($url) return $url;
$curlSES=curl_init();
curl_setopt($curlSES,CURLOPT_URL,"https://www.googleapis.com/books/v1/volumes?q=isbn:".urlencode($isbn));
curl_setopt($curlSES,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlSES,CURLOPT_HEADER, false);
$result=curl_exec($curlSES);
curl_close($curlSES);
$data=json_decode($result,true);
if(!$data) return "books.png";
$images=$data["items"][0]["volumeInfo"]["imageLinks"];
$lak = array_keys($images);
$le = $lak[count($lak)-1];
$url = $images[$le]?: "books.png";
cache_set($isbn,$url);
return $url;
}
?>