diff --git a/module/VuFind/src/VuFind/Content/Covers/Google.php b/module/VuFind/src/VuFind/Content/Covers/Google.php index 7d35d4062a7901af97bb78ff1a6513d499a030f4..05ec72a3612dbca8f336f012553e56d557806c7f 100644 --- a/module/VuFind/src/VuFind/Content/Covers/Google.php +++ b/module/VuFind/src/VuFind/Content/Covers/Google.php @@ -78,48 +78,27 @@ class Google extends \VuFind\Content\AbstractCover */ public function getUrl($key, $size, $ids) { - // Don't bother trying if we can't read JSON: - if (!is_callable('json_decode')) { + // Don't bother trying if we can't read JSON or ISBN is missing: + if (!is_callable('json_decode') || !isset($ids['isbn'])) { return false; } - if (!isset($ids['isbn'])) { - return false; - } - $isbn = $ids['isbn']->get13(); - - // Construct the request URL: - $url = 'http://books.google.com/books?jscmd=viewapi&' . - 'bibkeys=ISBN:' . $isbn . '&callback=addTheCover'; - // Make the HTTP request: + // Construct the request URL and make the HTTP request: + $url = 'https://books.google.com/books?jscmd=viewapi&' . + 'bibkeys=ISBN:' . $ids['isbn']->get13() . '&callback=addTheCover'; $result = $this->getHttpClient($url)->send(); - // Was the request successful? - if ($result->isSuccess()) { - // grab the response: - $json = $result->getBody(); - - // extract the useful JSON from the response: - $count = preg_match('/^[^{]*({.*})[^}]*$/', $json, $matches); - if ($count < 1) { - return false; - } - $json = $matches[1]; - + // If the request was successful and we can extract a valid response... + if ($result->isSuccess() + && preg_match('/^[^{]*({.*})[^}]*$/', $result->getBody(), $matches) + ) { // convert \x26 or \u0026 to & - $json = str_replace(["\\x26", "\\u0026"], "&", $json); - - // decode the object: - $json = json_decode($json, true); - - // convert a flat object to an array -- probably unnecessary, but - // retained just in case the response format changes: - if (isset($json['thumbnail_url'])) { - $json = [$json]; - } + $json = json_decode( + str_replace(['\\x26', '\\u0026'], '&', $matches[1]), true + ); // find the first thumbnail URL and process it: - foreach ($json as $current) { + foreach ((array)$json as $current) { if (isset($current['thumbnail_url'])) { return $current['thumbnail_url']; }