ajtaylordev
New Pleskian
Hi there, sorry I feel like I have posted way too much on this forum recently asking for help. I am so close to achieving a fully working and automated hosting account script using Plesk API. The issue I have now is: user clicks activate host account, Plesk API script creates everything as it should, PHP writes everything to my database as it should, however after the user has clicked activate host account, the page loads for a few seconds then returns a 503 error (even though the script ran correctly and everything was created.)
I was wondering if anybody knew what caused this, I'm just going to put the entire PHP page on here and hope that somebody can tell me where I am going wrong?
Thanks again:
I was wondering if anybody knew what caused this, I'm just going to put the entire PHP page on here and hope that somebody can tell me where I am going wrong?
Thanks again:
<?php
$new_plesk_error = false;
if(isset($_POST['activate_host'])) {
$plesk_name = $_POST['plesk_name'];
$plesk_company = $_POST['plesk_company'];
$plesk_email = $_POST['plesk_email'];
$plesk_user = $_POST['plesk_user'];
$plesk_pass = $_POST['plesk_pass'];
$service_plan = $_SESSION['service_plan'];
$plesk_created = $_SESSION['created'];
//Check for errors
if(empty($_POST['plesk_name'])) {
$new_plesk_error = true;
$_SESSION['ERROR_PLESK_NAME'] = "SET";
}
if($_POST["strength"] != "strong") {
$new_plesk_error = true;
$_SESSION['ERROR_PASS_STRENGTH'] = "SET";
}
if(empty($_POST['plesk_pass'])) {
$new_plesk_error = true;
$_SESSION['ERROR_NEW_PASS'] = "SET";
}
if(empty($_POST['plesk_email'])) {
$new_plesk_error = true;
$_SESSION['ERROR_NEW_EMAIL'] = "SET";
}
// Function to validate email
function test_input($resetemail) {
$resetemail = trim($resetemail);
$resetemail = stripslashes($resetemail);
$resetemail = htmlspecialchars($resetemail);
return $resetemail;
}
$resetemail = test_input($_POST["plesk_email"]);
// Validate email address
if (!filter_var($resetemail, FILTER_VALIDATE_EMAIL)) {
$new_plesk_error = true;
$_SESSION['ERROR_NEW_EMAIL_INVALID'] = "SET";
}
// ERROR CHECKS COMPLETE
if($new_plesk_error === false) { // NO ERRORS FOUND, PROCEED
// Update database to activate product for user
// Turn autocommit off
$link -> autocommit(FALSE);
$link -> query("UPDATE assoc_purchases SET item_status = 'enabled' WHERE created = '$plesk_created'");
// Commit transaction
if (!$link -> commit()) {
echo "Commit transaction failed";
}
///////////////////////////////////////////////////////////////////////////// START OF PLESK OPERATION
// First we need to create a secret key, to do that we will send a request with basic authentication.
$data = array(
'login' => 'admin',
'description' => 'Secret Key'
);
$payload = json_encode($data);
$login = 'XXXXXXXXX';
$password = 'XXXXXXXXXX';
$url = 'https://example-server.cloud/api/v2/auth/keys';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$secret_token = $result->key; // This is the key we will use for further authentication (no need for basic anymore).
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CREATE NEW CUSTOMER //
$url = 'https://example-server.cloud/api/v2/clients';
$data = array(
'name' => ''.$plesk_name.'',
'company' => ''.$plesk_company.'',
'login' => ''.$plesk_user.'',
'status' => '0',
'email' => ''.$plesk_email.'',
'locale' => 'en-US',
'owner_login' => 'admin',
'external_id' => 'link:12345',
'description' => 'User',
'password' => ''.$plesk_pass.'',
'type' => 'customer'
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'X-API-Key: '.$secret_token.'';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ADD SUBSCRIPTION PLAN TO CUSTOMER USING REST API CLI METHOD //
$url = 'https://example-server.cloud/api/v2/cli/subscription/call';
$payload = '{ "params": ["--create", "example.com", "-owner", "'.$plesk_user.'", "-service-plan", "'.$service_plan.'", "-ip", "82.165.XXX.XXX", "-login", "'.$plesk_user.'", "-passwd", "'.$plesk_pass.'"]}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-API-Key: '.$secret_token.'';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
unset($_SESSION['created']);
unset($_SESSION['service_plan']);
header("location:account.php?navigation=products");
} else {
$_SESSION['ERROR_PLESK_FAILURE'] = "SET";
}
}
?>