2

I'm writing a script to add a new user to the hosting control panel "VESTA". I write according to the manual for api: http://vestacp.com/docs/api/ Errors when executing the code does not produce, but the user is not added to the VESTA. My code:

$vst_hostname = 'dfsdfl.csfdsdsgds.com';

$postvars = array(
$vst_username = 'zdeslogin',
$vst_password = 'zdesparol',
$vst_returncode = 'yes',
$vst_command = 'v-add-user',
$username = 'demo',
$password = 'password',
$email = 'demo@gmail.com',
$package = 'default',
$fist_name = 'Rust',
$last_name = 'Cohle'
);

$postdata = http_build_query($postvars);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
    echo "User account has been successfuly created\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}

After running the code, the following line is returned: User account has been successfuly created.

Spartak
  • 129
  • 7

1 Answers1

0

The Russian community helped. Here is the code:

<?php

// Server credentials
$vst_hostname = 'sadsad.sadsad.com';

$vst_username = 'zdeslogin';
$vst_password = 'zdesparol';
$vst_returncode = 'no';
$vst_command = 'v-add-user';
$username = 'demo3';
$password = 'd3m0p4ssw0rd';
$email = 'demo@gmail.com';
$package = 'default';
$fist_name = 'Rust';
$last_name = 'Cohle';

$postvars = array(
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
    'arg1' => $username,
    'arg2' => $password,
    'arg3' => $email,
    'arg4' => $package,
    'arg5' => $fist_name,
    'arg6' => $last_name
);
$postdata = http_build_query($postvars);

// Send POST query via cURL
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);

// Check result
if($answer == 0) {
    echo "User account has been successfuly created\n";
} else {
    echo "Query returned error code: " .$answer. "\n";
}

?>
Spartak
  • 129
  • 7
  • 1
    You might want to clarify what the problem and what the solution actually were. Your code looks pretty much the same to me. – Devon Bessemer Jul 20 '18 at 11:20
  • api VESTA is rigidly attached to keys in associative arrays. The keys should be exactly the same as in the documentation. – Spartak Jul 20 '18 at 12:16
  • Also, I do not exclude that api is also rigidly bound to the name of the variables specified in the array. – Spartak Jul 20 '18 at 12:19
  • Makes sense, I didn't notice how malformed your array was in your question, just saw you posted an answer of code without any description. I think you'll find this common with just about any API, you can't just pass the data you want, you have to conform to their key names. Just a heads up, you have a redundant`$postdata = http_build_query($postvars);`. – Devon Bessemer Jul 20 '18 at 12:49