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);

$client->setCredentials($login, $password);

After that, prepare a packet:

$request = <<<EOF
<packet>
  <server>
    <get_protos/>
  </server>
</packet>
EOF;

And then perform a request:

$response = $client->request($request);

echo $response;

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="1.6.5.0">
  <server>
    <get_protos>
      <result>
        <status>ok</status>
        <protos>
          <proto>1.0.0.0</proto>
          <proto>1.1.0.0</proto>
          ....
          <proto>1.6.5.0</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.