From a603d3e75f67b5641b1049b5b53de2f6bf035a88 Mon Sep 17 00:00:00 2001 From: Filip Hendrik Date: Thu, 30 Mar 2017 02:13:20 +0200 Subject: [PATCH] Throw exceptions on error, check that we have results and always close the curl connection. --- LBRY.class.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/LBRY.class.php b/LBRY.class.php index 85906bef..e3800e93 100644 --- a/LBRY.class.php +++ b/LBRY.class.php @@ -13,22 +13,27 @@ class LBRY curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['method' => $function, 'params' => $params])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $server_output = curl_exec($ch); + $serverOutput = curl_exec($ch); + curl_close($ch); - if ($server_output) + if ($serverOutput) { - $responseData = json_decode($server_output, true); - return $responseData['result']; + $responseData = json_decode($serverOutput, true); + + if (isset($responseData['error'])) { + throw new Exception($responseData['error']); + } + + if (isset($responseData['result'])) { + return $responseData['result']; + } } - - curl_close ($ch); - - return $server_output; } public static function findTopPublicFreeClaim($name) { $claims = LBRY::api('claim_list', ['name' => $name]); + if (!$claims || !isset($claims['claims'])) { return null;