Etiket arşivi: plesk host

Parallels Plesk Panel API RPC Protocol Developer


class ApiRequestException Extends the standard Exception class. Visit for details.
function domainsInfoRequest() Uses the DOM model to compose a request XML packet. Returns the resulting DOM object.
function curlInit() Initializes the CURL session with necessary options as follows:
CURLOPT_URL specifies the destination URL
CURLOPT_RETURNTRANSFER set to true means that the resulting output will be returned from the server in the form of a string.
CURLOPT_POST set to true means that the packet will be sent via HTTP POST.
CURLOPT_SSL_VERIFYPEER set to false stops CURL from verifying the peer’s certificate.
CURLOPT_SSL_VERIFYHOST set to false stops CURL from verifying the host.
CURLOPT_HTTPHEADER specifies an array of HTTP header fields to set.
Returns the handler of the URL session.
function sendRequest() Sends the HTTP packet by means of CURL and gets the pure XML response packet (without the HTTP header). Closes the CURL session and returns the resulting packet (a plain text with XML tags). To learn more about the CURL engine, visit
function parseResponse() Gets the response packet (plain text) as a parameter. Parses the packet using SimpleXML and returns the SimpleXMLElement object in which the packet is structured as a tree. To learn more about the SimpleXML extension of PHP, visit
function checkResponse() Gets the response packet of type SimpleXMLEelement and checks the result node. If this node holds ‘error’, then the function throws an exception with a string passed in the errtext node.

The main() function invokes these functions in the order they are considered. First it calls domainInfoRequest() to form a packet as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<packet version="">

Then curlInit() is called to initialize CURL session with options. The sendRequest function sends a packet with the HTTP header as follows:

POST /enterprise/control/agent.php HTTP/1.1
Content-Length: 294
Content-Type: text/xml

The response XML packet received from Plesk server can look as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<packet version="">

Once the response packet is received and parsed with the parseResponse() function, the foreach iterator echoes the contents of the result node.

Code sample


 * Reports error during API RPC request
class ApiRequestException extends Exception {}

 * Returns DOM object representing request for information about all available domains
 * @return DOMDocument

function domainsInfoRequest()
      $xmldoc = new DomDocument('1.0', 'UTF-8');
      $xmldoc->formatOutput = true;

      // <packet>
      $packet = $xmldoc->createElement('packet');
      $packet->setAttribute('version', '');

      // <packet/domain>
      $domain = $xmldoc->createElement('domain');

      // <packet/domain/get>
      $get = $xmldoc->createElement('get');

      // <packet/domain/get/filter>
      $filter = $xmldoc->createElement('filter');

      // <packet/domain/get/dataset>
      $dataset = $xmldoc->createElement('dataset');

      // dataset elements

      return $xmldoc;
 * Prepares CURL to perform Plesk API request
 * @return resource
function curlInit($host, $login, $password)
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST,           true);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curl, CURLOPT_HTTPHEADER,
             array("HTTP_AUTH_LOGIN: {$login}",
                    "HTTP_AUTH_PASSWD: {$password}",
                    "HTTP_PRETTY_PRINT: TRUE",
                    "Content-Type: text/xml")

      return $curl;
 * Performs a Plesk API request, returns raw API response text
 * @return string
 * @throws ApiRequestException
function sendRequest($curl, $packet)
      curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
      $result = curl_exec($curl);
      if (curl_errno($curl)) {
             $errmsg  = curl_error($curl);
             $errcode = curl_errno($curl);
             throw new ApiRequestException($errmsg, $errcode);
      return $result;

 * Looks if API responded with correct data
 * @return SimpleXMLElement
 * @throws ApiRequestException
function parseResponse($response_string)
      $xml = new SimpleXMLElement($response_string);
      if (!is_a($xml, 'SimpleXMLElement'))
             throw new ApiRequestException("Cannot parse server response: {$response_string}");
      return $xml;
 * Check data in API response
 * @return void
 * @throws ApiRequestException
function checkResponse(SimpleXMLElement $response)
      $resultNode = $response->domain->get->result;

      // check if request was successful
      if ('error' == (string)$resultNode->status)
             throw new ApiRequestException("Plesk API returned error: " . (string)$resultNode->result->errtext);

// int main()
$host = '';
$login = 'admin';
$password = 'qwedsa';

$curl = curlInit($host, $login, $password);

try {

      $response = sendRequest($curl, domainsInfoRequest()->saveXML());
      $responseXml = parseResponse($response);

} catch (ApiRequestException $e) {
      echo $e;

// Explore the result
foreach ($responseXml->xpath('/packet/domain/get/result') as $resultNode) {
      echo "Domain id: " . (string)$resultNode->id . " ";
      echo (string)$resultNode->data->gen_info->name . " (" . (string)$resultNode->data->gen_info->dns_ip_address . ")\n";