• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Script PHP for creating subdomain

JulienA26

New Pleskian
Hello,
I am trying to write a script PHP creating a subdomain on a server when executed (as well as doing other stuff, but that part work).
But, when I send my request to the server, the script, while it does create the subdomain, doesn't display anything upon execution (I have put a few echo in the script to check the advancement, and nothing appears).
The error message : Echec du chargement de la page Aucune donnée reçue.
Here is my code :

/**
* Returns DOM object representing request for creation of new subdomain named after the parameter
* @return DOMDocument
*/
function createSubdomain($nom)
{
$xmldoc = new DomDocument('1.0', 'UTF-8');
$xmldoc->formatOutput = true;
$packet = $xmldoc->createElement('packet');
$packet->setAttribute('version', '1.5.2.0');
$xmldoc->appendChild($packet);
$subdomain = $xmldoc->createElement('subdomain');
$packet->appendChild($subdomain);
$add = $xmldoc->createElement('add');
$subdomain->appendChild($add);
$parent = $xmldoc->createElement('parent', 'siteweb.net');
$add->appendChild($parent);
$name = $xmldoc->createElement('name', $nom);
$add->appendChild($name);
$add->appendChild($xmldoc->createElement('home', '/'.$nom.''));
$property = $xmldoc->createElement('property');
$property->appendChild($xmldoc->createElement('name', 'ssi'));
$property->appendChild($xmldoc->createElement('value', 'true'));
$add->appendChild($property);
return $xmldoc;

}
And the main part :

$host = 'XXX.XXX.XXX.XXX';
$login = 'admin';
$password = 'XXXXXX';

echo "<br/>";
$curl7 = curlInit($host, $login, $password);
$contenu = createSubDomain('Essai')->saveXML();
echo htmlspecialchars($contenu);
$reponse = sendRequest($curl7, $contenu);
echo "<br/>";
echo htmlspecialchars($reponse);
echo "<br/>";
echo "check";

The function curlInit and sendRequest are those used in the examples provided on this site, but I put them here just in case :

/**
* 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;
}

Can anyone please tell me why it doesn't send anything back ? When I modify the property line in the request, the script displays something, but it also fails at creating the subdomain (because the parameters are incorrect).
I am new at this, so it is probably something fairly obvious.
Thanks in advance.
 
I dont think plesk will allow external script.It requires plesk permssion to create the subdomain.
 
Hi ! Did you finally found a solution? I have the same problem you got, so if you're able to help me a bit !

Thanks!
 
Back
Top