• Halloween is coming! To celebrate the occasion, we dusted off a couple of old skins, and even created a brand new one (Pixel).
    To get into the festive spirit, install the Skins extension.
  • The new Python extension is now available. It allows customers to deploy and manage WSGI-based Python applications on their websites directly from Plesk.
  • The Plesk Mobile apps for iOS and Android and the Plesk Mobile Center extension have been deprecated, and are no longer available for new installations. In Plesk Obsidian 18.0.84, the Plesk Mobile Center extension will be removed from servers during the upgrade, and the Plesk Mobile apps will lose their server management features.
  • The Social Login extension has been deprecated and will be removed in Plesk Obsidian 18.0.84. After that, it will no longer be possible to log in to Plesk with a Google, Facebook, GitHub account, or with a passkey. Plesk usernames and passwords, as well as logging in with a WebPros Account, are not affected.

General API RPC advice please

A

Atari Boy

Guest
Hi,

I new to all this but working hard to try and understand.

I am trying to get to grips with the API RPC via PHP on the following setup:
Parallels Plesk Panel v10.4.4_build1013111102.18 os_CentOS 6

I have been using the example code in the documentation as follows:



------------------------------------------------------------------------------
<?php

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

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

function createSubdomain() //this function is called further down
{
$xmldoc = new DomDocument('1.0', 'UTF-8');
$xmldoc->formatOutput = true;
// <packet>
$packet = $xmldoc->createElement('packet');
$packet->setAttribute('version', '1.6.2.0');
$xmldoc->appendChild($packet);
// <packet/domain>
$domain = $xmldoc->createElement('domain');
$packet->appendChild($domain);
// <packet/domain/get>
$get = $xmldoc->createElement('get');
$domain->appendChild($get);

// <packet/domain/get/filter>
$filter = $xmldoc->createElement('filter');
$get->appendChild($filter);
// <packet/domain/get/dataset>
$dataset = $xmldoc->createElement('dataset');
$get->appendChild($dataset);
// dataset elements
$dataset->appendChild($xmldoc->createElement('hosting'));
$dataset->appendChild($xmldoc->createElement('gen_info'));
return $xmldoc;
}

/**
* Prepares CURL to perform the Panel 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 Panel 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);
curl_close($curl);
throw new ApiRequestException($errmsg, $errcode);
}
curl_close($curl);
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("The Panel API returned an
error: " . (string)$resultNode->result->errtext);
}

//
// int main()
//
$host = 'xxx.xxx.xxx.xxx';
$login = 'admin';
$password = 'xxxxxxxxxxxxxxxxxxx';

$curl = curlInit($host, $login, $password);
try {
$response = sendRequest($curl, createSubdomain()->saveXML());
$responseXml = parseResponse($response);
checkResponse($responseXml);
} catch (ApiRequestException $e) {
echo $e;
die();
}

// 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";
}

?>

------------------------------------------------------------------------------



This works fine. However in the API RPC 1.6.3.1 Reference and other forum posts I see people quoting XML examples like this:


<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.6.3.3">
<subdomain>
<add>
<parent>domain.com</parent>
<name>test</name>
</add>
</subdomain>
</packet>



Why is the above used when in the PHP example given the XML is structured differently.

I obviously have a gap in my knowledge, can someone please explain and give me a nudge in the right direction?

Many thanks in advance.

Atari Boy
 
Back
Top