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="22.214.171.124"> <server> <get_protos> <result> <status>ok</status> <protos> <proto>126.96.36.199</proto> <proto>188.8.131.52</proto> .... <proto>184.108.40.206</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 feedback on this topic here
If you have questions or need support, please visit the Plesk forum or contact your hosting provider.
The comments below are for feedback on the documentation only. No timely answers or help will be provided.