diff --git a/phpcclib.php b/phpcclib.php index 3250b14..4a7199d 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1,4 +1,5 @@ $addonName); return $this->_executePost($resource, $data); } - + /** * get sso login data @@ -976,7 +977,7 @@ public function billingAccount_getVoucherList($userName, $billingName) public function addon_getList() { return $this->_executeGet('/addon/', $requiresToken=false); } - + /** * return list of all support plans * @@ -993,12 +994,12 @@ public function addon_getList() { public function support_getList() { return $this->_executeGet('/support/', $requiresToken=false); } - + /** * return a single support plan * * @param string $planName name of the support plan - * + * * @throws BadRequestError * @throws ForbiddenError * @throws GoneError @@ -1181,6 +1182,39 @@ private function _jsonDecode($content) { */ class CCException extends Exception { + public function __construct($message, $status) + { + $this->message = $message; + + $obj = json_decode($message); + if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) { + $rc = ""; + $errors = array(); + foreach ($obj as $k => $v) { + if ($k == "rc") + $rc = $v; + else if ($k == "error") + $errors = $v; + } + + $this->message = ''; + + if (strlen($rc) > 0) + $this->message .= "$rc\n"; + + if (count($errors) > 0) { + foreach ($errors as $k => $v) { + $this->message .= sprintf("%s: %s\n", $k, $v); + } + } + + if ($this->message == '') { + foreach ($obj as $k => $v) { + $this->message .= sprintf("%s: %s\n", $k, $v); + } + } + } + } } /* @@ -1198,10 +1232,9 @@ class ConnectionException extends CCException { */ class TokenRequiredError extends CCException { - public function __toString() - { - return 'No valid token. Use create_token(email, password) to get a new one'; - } + public function __construct() { + parent::__construct("No valid token. Use create_token(email, password) to get a new one", 0); + } } /* @@ -1210,24 +1243,12 @@ public function __toString() */ class BadRequestError extends CCException { - private $_msgs = array(); - - public function __construct($message) + public function __construct($message, $status) { - $this->message = 'BadRequest'; - /* - * You will get a string like this - * Bad Request {"lastname": "This field is required.", "firstname": "This field is required."} - * therefore we cut the first 12 chars from errorMessage - */ - $obj = json_decode(substr($message, 12)); - - if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) { - $this->message = ''; - foreach ($obj as $k => $v) { - $this->message .= sprintf("%s: %s\n", $k, $v); - } - } + if (substr($message, 0, 11) == "Bad Request") + parent::__construct(substr($message, 12), $status); + else + parent::__construct($message, $status); } } @@ -1439,17 +1460,18 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar $request = new HTTP_Request2($url); $request->setConfig('ssl_verify_peer', API::SSL_VERIFY_PEER); - $methods = array( - 'options' => HTTP_Request2::METHOD_OPTIONS, - 'get' => HTTP_Request2::METHOD_GET, - 'head' => HTTP_Request2::METHOD_HEAD, - 'post' => HTTP_Request2::METHOD_POST, - 'put' => HTTP_Request2::METHOD_PUT, - 'delete' => HTTP_Request2::METHOD_DELETE, - 'trace' => HTTP_Request2::METHOD_TRACE, - 'connect' => HTTP_Request2::METHOD_CONNECT - ); - $request->setMethod($methods[strtolower($method)]); + //$methods = array( + //'options' => HTTP_Request2::METHOD_OPTIONS, + //'get' => HTTP_Request2::METHOD_GET, + //'head' => HTTP_Request2::METHOD_HEAD, + //'post' => HTTP_Request2::METHOD_POST, + //'put' => HTTP_Request2::METHOD_PUT, + //'delete' => HTTP_Request2::METHOD_DELETE, + //'trace' => HTTP_Request2::METHOD_TRACE, + //'connect' => HTTP_Request2::METHOD_CONNECT + //); + //$request->setMethod($methods[strtolower($method)]); + $request->setMethod($method); # # If the current API instance has a valid token we add the Authorization @@ -1475,9 +1497,8 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar $url = $request->getUrl(); $url->setQueryVariables($data); } else { - // works with post and put - $request->addPostParameter($data); - $request->setBody(http_build_query($data)); + $request->setBody(json_encode($data)); + $headers['Content-Type'] = 'application/json'; } } @@ -1487,18 +1508,12 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar # the wild. # $headers['User-Agent'] = sprintf('phpcclib/%s', $this->_version); - # - # The API expects PUT or POST data to be x-www-form-urlencoded so we - # also set the correct Content-Type header. - # - if (strtoupper($method) == 'PUT' || strtoupper($method) == 'POST') { - $headers['Content-Type'] = 'application/x-www-form-urlencoded'; - } - # + # # We also set the Content-Length and Accept-Encoding headers. # //$headers['Content-Length'] = strlen($body); $headers['Accept-Encoding'] = 'compress, gzip'; + $headers['Accept'] = 'application/json'; # # Finally we fire the actual request. #