Comments
|
Extends the standard Exception class. Visit http://www.php.net/manual/en/language.exceptions.php for details. | |
|
Uses the DOM model to compose a request XML packet. Returns the resulting DOM object. | |
|
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. | ||
|
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 http://www.php.net/manual/en/ref.curl.php. | |
|
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 http://www.php.net/manual/en/ref.simplexml.php. | |
|
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="1.4.1.2">
<domain>
<get>
<filter/>
<dataset>
<limits/>
<prefs/>
<user/>
<hosting/>
<stat/>
<gen_info/>
</dataset>
</get>
</domain>
</packet>
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
Host: 10.58.32.100:8443
HTTP_AUTH_LOGIN: login
HTTP_AUTH_PASSWD: qwedsa
HTTP_PRETTY_PRINT: TRUE
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="1.4.1.2">
<domain>
<get>
<result>
<status>ok</status>
<id>1234</id>
<value>
<gen_info>
<cr_date>1154513574</cr_date>
<name>technolux.co.uk</name>
<display_name>technolux</display_name>
<status>64</status>
<real_size>0</real_size>
<client_id>4610</client_id>
<dns_ip_address>123.123.123.121</dns_ip_address>
<htype>none</htype>
</gen_info>
</value>
</result>
</get>
</domain>
</packet>
Once the response packet is received and parsed with the parseResponse()
function, the foreach
iterator echoes the contents of the result node.
Code sample
<?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 domainsInfoRequest()
{
$xmldoc = new DomDocument('1.0', 'UTF-8');
$xmldoc->formatOutput = true;
// <packet>
$packet = $xmldoc->createElement('packet');
$packet->setAttribute('version', '1.4.1.2');
$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('limits'));
$dataset->appendChild($xmldoc->createElement('prefs'));
$dataset->appendChild($xmldoc->createElement('user'));
$dataset->appendChild($xmldoc->createElement('hosting'));
$dataset->appendChild($xmldoc->createElement('stat'));
$dataset->appendChild($xmldoc->createElement('gen_info'));
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);
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("Plesk API returned error: " . (string)$resultNode->result->errtext);
}
//
// int main()
//
$host = '10.58.32.100';
$login = 'admin';
$password = 'qwedsa';
$curl = curlInit($host, $login, $password);
try {
$response = sendRequest($curl, domainsInfoRequest()->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";
}
?>