Book Contents

Previous Topic

Next Topic

Sample in PHP

To create a sample domain myowntest.domain.com for Client 1 on the basis of Domain template 1, with enabled anonymous FTP, follow these steps:

<?php

function write_callback($ch, $data) {

     echo $data;

     return strlen($data);

}

function sendCommand($operator, $data, $login, $passwd, $host, $port=8442) {

     $script = "webgate.php";

     $url = "https://$host:$port/$script";

     $headers = array(

          "HTTP_AUTH_OP: $operator",

          "HTTP_AUTH_LOGIN: $login",

          "HTTP_AUTH_PASSWD: $passwd",

          "Content-Type: text/xml",

     );

     $ch = curl_init();

     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

     curl_setopt($ch, CURLOPT_HTTPHEADER, &$headers);

     curl_setopt($ch, CURLOPT_URL, $url);

     curl_setopt($ch, CURLOPT_WRITEFUNCTION, write_callback);

     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

     curl_setopt($ch, CURLOPT_VERBOSE, 1);

     $result = curl_exec($ch);

     if (!$result) {

          echo "\n\n-------------------------\ncURL error number:".curl_errno($ch);

          echo "\n\ncURL error:".curl_error($ch);

     }

     curl_close($ch);

     return;

}

$data = <<<EOF

<?xml version="1.0" encoding="UTF-8"?>

<!-- Get plesk servers list -->

<packet version="2.0.1.2">

<get>

<filter></filter><!-- select all Plesk servers -->

<dataset>

<gen_info></gen_info>

</dataset>

</get>

</packet>

EOF;

$operator="exp_plesk_domain";

$login="root";

$passwd="setup";

$host="server1.com";

$port=8442;

sendCommand($operator, $data, $login, $pass, $host, $port);

?>

For detailed explanations on the syntax and parameters of this statement, see Variables and Operations and Comments further in this subsection.

Variables

In this statement, replace the following variables with the appropriate content:

Variable

Description

$host

Provide the name or IP address of the Plesk host on which you wish to create a domain.

$port

The standard port used for communication with Plesk Expand. The default meaning is 8442; if you have set another port for communication, specify it here.

$login

Provide your Plesk Expand login.

$passwd

Provide your Plesk Expand password.

$operator

Place the XML packet containing the operation parameters. Provide the name of the Plesk Management Operator responsible for domain creation. For the full list of operators, refer to section Plesk Management Operators.

For domain management operations, exp_plesk_domain is used.

Operations and comments

In this statement, the following functions/groups of functions perform the following tasks:

Operation / group of operations

Action performed

function write_callback($ch, $data) {

echo $data;

return strlen($data);

}

Declaring function: getting server reply to stdout.

function sendCommand($operator, $data, $login, $passwd, $host, $port=8442)

Declaring function: generate and send the packet to Plesk Expand.

$script = "webgate.php";

$url = "https://$host:$port/$script";

A Plesk Expand script used for remote calls. The URL is composed from the described variables and commands:

$host, $port and $script.

$headers = array(

"HTTP_AUTH_OP: $operator",

"HTTP_AUTH_LOGIN: $login",

"HTTP_AUTH_PASSWD: $passwd",

"Content-Type: text/xml",

);

Creating packet header.

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, &$headers);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_WRITEFUNCTION, write_callback);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

Initializing the curl engine: setting HTTPS parameters, packet header ($headers), specifying the URL to be processed ($url), callback functions, etc.

$result = curl_exec($ch);

Sending data to the remote server and requesting output.

$data = <<<EOF

<...>

EOF;

The XML packet providing the parameters of the domain to be created.

Variables in the XML packet: <name>, <client_id>, <tmpl_id> and others are explained in section Sample Statement. Creating Domain Using XML API.

sendCommand($operator, $data, $login, $pass, $host, $port);

Calling the function to create and send the packet to the remote server.