From 6c881abc14b6448deb4fa5702959d54e8c5dcc78 Mon Sep 17 00:00:00 2001 From: Adnan RIHAN Date: Sun, 8 Sep 2019 23:58:12 +0200 Subject: [PATCH 1/2] runQuery now returns json decoded response, or throw an Exception --- src/Gufy/CpanelPhp/Cpanel.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Gufy/CpanelPhp/Cpanel.php b/src/Gufy/CpanelPhp/Cpanel.php index c8f978f..01f9fba 100644 --- a/src/Gufy/CpanelPhp/Cpanel.php +++ b/src/Gufy/CpanelPhp/Cpanel.php @@ -330,6 +330,8 @@ private function createHeader() * @param string $arguments list of parameters that will be attached. * @param bool $throw defaults to false, if set to true rethrow every exception. * + * @throws Exception|GuzzleHttp\Exception\ClientException + * * @return array results of API call * * @since v1.0.0 @@ -348,7 +350,11 @@ protected function runQuery($action, $arguments, $throw=false) 'connect_timeout' => $this->getConnectionTimeout() ]); - return (string) $response->getBody(); + if (($decodedBody = json_decode($response->getBody(), true)) === false) { + throw new \Exception(json_last_error_msg(), json_last_error()); + } + + return $decodedBody; } catch(\GuzzleHttp\Exception\ClientException $e) { From 36535c80771b1547d44c909dddd5a3134ad5487d Mon Sep 17 00:00:00 2001 From: Adnan RIHAN Date: Mon, 9 Sep 2019 01:27:52 +0200 Subject: [PATCH 2/2] Added Whm class, calling api.version=1, throws by default --- src/Gufy/CpanelPhp/Whm.php | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Gufy/CpanelPhp/Whm.php diff --git a/src/Gufy/CpanelPhp/Whm.php b/src/Gufy/CpanelPhp/Whm.php new file mode 100644 index 0000000..8c5cbe9 --- /dev/null +++ b/src/Gufy/CpanelPhp/Whm.php @@ -0,0 +1,74 @@ + + * + * @version v1.0.0 + * + * @link https://github.com/max13/cpanel-php + * @since v2.0.0 + */ +class Whm extends Cpanel +{ + /** + * Magic method who will call the WHM Api 1. + * + * @param string $function function name that will be called + * @param array $arguments parameter that should be passed when calling API function + * + * @return array result of called functions + * + * @since v2.0.0 + */ + public function __call($function, $arguments) + { + if (count($arguments) === 0) { + $arguments[0] = []; + } + + if (!array_key_exists('api.version', $arguments[0])) { + $arguments[0]['api.version'] = 1; + } + + array_unshift($arguments, $function); + + return call_user_func_array([$this, 'runQuery'], $arguments); + } + + /** + * The executor. It will run API function and get the data. + * + * @param string $action function name that will be called. + * @param string $arguments list of parameters that will be attached. + * @param bool $throw defaults to true, if set to false: returns error message + * + * @return array results of API call + * + * @throws Exception + * + * @since v2.0.0 + */ + protected function runQuery($action, $arguments, $throw = true) + { + try { + $response = parent::runQuery($action, $arguments, true); + + if (!$response['metadata']['result'] && $throw) { + throw new \Exception($response['metadata']['reason']); + } + + return $response['data']; + } catch (\Exception $e) { + if ($throw) { + throw $e; + } + + return $e->getMessage(); + } + + } +}