/** * Select out the simple result string * * This is primarily for use by legacy methods within * Cpanel_Service_Adapter_Liveapi * * @param Cpanel_Query_Object $rObj Response object * * @return mixed Value stored in the data->result node of server response */ public function getLegacyString($rObj) { if ($rObj->validResponse()) { return $rObj->cpanelresult->data->result; } else { return null; } }
/** * Build fopen's context option array * * @param Cpanel_Query_Object $rObj Response object to source and * update data as necessary * * @return array Array of elements appropriate for use with PHP's * "stream_context_create()" function * @throws Exception If $rObj is an invalid object */ protected function buildFopenContextOpts($rObj) { if (!$rObj instanceof Cpanel_Query_Object) { throw new Exception(__FUNCTION__ . ' requires a QueryObject argument'); } $queryObj = $rObj->getQuery(); $postdata = $queryObj->args; $authstr = $queryObj->authstr; $opts = array('http' => array('allow_self_signed' => true, 'method' => 'POST', 'header' => '')); // Make general headers $headers = array('Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => strlen($postdata)); // Merge any custom headers $h = $queryObj->httpHeaders; if ($h) { $customHeaders = $h->getAllDataRecursively(); $headers = $customHeaders + $headers; } foreach ($headers as $key => $value) { $headerStrs[] = "{$key}: {$value}"; } $fopenHeaderStr = $authstr . implode("\r\n", $headerStrs); $qt = $queryObj->httpQueryType; if ($qt && strtoupper($qt) == 'GET') { $queryObj->url = "{$queryObj->url}?{$postdata}"; $opts['http']['method'] = $qt; } else { $fopenHeaderStr .= "\r\n{$postdata}"; } $opts['http']['header'] = $fopenHeaderStr; return $opts; }
/** * Handle special modes where output is expected to be something other than * the default Cpanel_Query_Object * * @param Cpanel_Query_Object $rObj Response object * * @return mixed * @throws Exception If invalid input */ private function _returnOutputMode($rObj) { if (!$rObj instanceof Cpanel_Query_Object) { throw new Exception('Invalid response object'); } $r = $rObj; if ($rObj->validResponse()) { if ($this->mode & self::LEGACY_STRING_MODE) { $r = $rObj->getResponseParser()->getLegacyString($rObj); } } return $r; }
/** * Encode array structure into this parser's format type * * Encoding is only performed with DOMDocument. Returned XML string will * be formated with line breaking and whitespace characters. * * @param Cpanel_Query_Object $obj Response object containing data to * encode * * @see Cpanel_Parser_Interface::encodeQueryObject() * * @return string XML document * @throws Exception If $obj is an invalid response object */ public function encodeQueryObject($obj) { if (!$obj instanceof Cpanel_Query_Object) { throw new Exception('Parser can only encode known query object'); } $arr = $obj->getResponse('array'); $this->_dom = new DOMDocument('1.0'); $this->_dom->preserveWhiteSpace = false; $this->_dom->formatOutput = true; // TODO: add error detection for empty arr if (count($arr) > 1) { $root = $this->_dom->createElement('result'); $this->_dom->appendChild($root); $this->_recurse_node_build($arr, $root); } elseif (count($arr) == 1) { $this->_recurse_node_build($arr, $this->_dom); } $r = $this->_dom->saveXML(); unset($this->_dom); return $r; }
/** * Encode array structure into this parser's format type * * If Cpanel_Parser_JSON::EXPANDED_MODE was set via {@link setMode()} * then the returned string will have excessive whitespace and line breaking * characters * * NOTE: unlike {@link parse()}, this method WILL throw an Exception If * encoding fails. There should be no ambiguity, if the response objects * "_response" container has invalid data, there is a problem (that needs * to be addressed by the script/application developer). * * @param Cpanel_Query_Object $obj Response object containing data to * encode * * @see Cpanel_Parser_Interface::encodeQueryObject() * * @return string JSON string * @throws Exception If $obj is an invalid response object * @throws Exception If encoding fails */ public function encodeQueryObject($obj) { if (!$obj instanceof Cpanel_Query_Object) { throw new Exception('Parser can only encode known query object'); } $arr = $obj->getResponse('array'); $jsonStr = ''; $msg = ''; try { $jsonStr = json_encode($arr); } catch (Exception $e) { $msg = ". " . $e->getMessage(); $msg .= '. Store data likely contains a resource' . ' or non-UTF8 string.'; } if (!empty($msg) || empty($jsonStr)) { throw new Exception('JSON encoding error for ' . __FUNCTION__ . $msg); } if ($this->mode == self::EXPANDED_MODE) { $jsonStr = $this->prettyPrint($jsonStr); } return $jsonStr; }
* the PHP SimpleXML functions to decode the string */ $simplxml = simplexml_load_string($raw); echo "WHM Version: {$simplxml->version}\n"; /** * The following example illustrates a direct URL query against * $server:2087/json-api/listaccts?serchtype=domain&search=dave.com * and passing a custom header 'CustomHeader=CustomSendValue' */ $url = '/json-api/listaccts'; $formdata = array('searchtype' => 'domain', 'search' => 'dave.com'); $response = $cp->api_request('WHM', $url, 'GET', $formdata, array('CustomHeader' => 'CustomSendValue')); $raw = $response->getRawResponse(); /** * Again, decode appropriately. * * Here, we'll use the cPanel PHP library's JSON parser class */ $newResponse = new Cpanel_Query_Object(); $newResponse->setResponseFormatType('JSON'); $newResponse->parse($raw); echo "Dave's Domain Detail:\n"; foreach ($newResponse->acct as $acct) { foreach ($acct as $key => $value) { echo "\t{$key}: {$value}\n"; } } } catch (Exception $e) { echo $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; } echo "EOF: You've successfully ended the " . basename(__FILE__) . " script.\n";