From 4dcd93b94763cd4bdcbaa2617f63b6846d500125 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 12 Nov 2024 15:36:32 +0100 Subject: [PATCH] Better validate input JSON fetched from Wikipedia --- src/NodaWikidataFetcher.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/NodaWikidataFetcher.php b/src/NodaWikidataFetcher.php index 31d35a6..a9fcab4 100644 --- a/src/NodaWikidataFetcher.php +++ b/src/NodaWikidataFetcher.php @@ -151,7 +151,11 @@ final class NodaWikidataFetcher { private static function _getCleanedWikipediaSnippet(string $lang, string $title):string { $datafromwiki = MD_STD::runCurl(self::_getWikipediaApiLink($lang, $title), 10000); - $datafromwiki = strval(json_decode($datafromwiki, true)['parse']['text']['*']); + $json_decoded = json_decode($datafromwiki, true); + if (empty($json_decoded) || !isset($json_decoded['parse'])) { + return ''; + } + $datafromwiki = strval($json_decoded['parse']['text']['*']); return self::_cleanWikidataInput($datafromwiki);