Sending Request Packets
When an XML API packet is ready, it should be wrapped into an HTTP header and sent to the specified server. To perform these tasks, we suggest using any HTTP/FTP client library. This topic demonstrates how this can be done in PHP using the CURL engine - a free and open client-side URL transfer library.
To simplify working with XML API, you can use the PleskApiClient class. To prepare an XML API client, you need to create an instance of PleskApiClient and initialize it:
$client = new PleskApiClient($host);
After that, prepare a packet:
$request = <<<EOF <packet> <server> <get_protos/> </server> </packet> EOF;
And then perform a request:
$response = $client->request($request);
You should be able to see a response for the “get_protos” command, similar to the following:
<?xml version="1.0" encoding="UTF-8"?> <packet version="184.108.40.206"> <server> <get_protos> <result> <status>ok</status> <protos> <proto>220.127.116.11</proto> <proto>18.104.22.168</proto> .... <proto>22.214.171.124</proto> </protos> </result> </get_protos> </server> </packet>
These XML data can be parsed by an XML parser such as SimpleXML or DOMDocument.
The PleskApiClient class allows performing authorization by using a secret key instead of a username and password pair. For example:
$client->setSecretKey('<here is the key>');
Internally the PleskApiClient class uses curl to perform a request. It defines the URL, sets the necessary headers and sets the request body:
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "$protocol://$host:$port/enterprise/control/agent.php"); ... curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_getHeaders()); curl_setopt($curl, CURLOPT_POSTFIELDS, $request); $result = curl_exec($curl); curl_close($curl);
The code of this example and the PleskApiClient class are located at https://github.com/plesk/api-examples/tree/master/php.
Leave your comments on this page
Leave your feedback or question on this documentation topic below. For technical assistance, contact your hosting service provider or submit a request to Plesk support. Suggest new features for Plesk here. Discuss general questions on the Plesk forum. All offtopic comments will be removed.