2015-08-15 19 views
0

我想設置一個用戶創建的API VestaCP。我有這兩頁。如何把POST表單數據在行動

形式:

<form action="process.php" method="post"> 
Username:<br> 
<input type="text" name="username"> 
<br> 
Password:<br> 
<input type="password" name="password"> 
<br> 
Email Address:<br> 
<<input type="email" name="email"> 
<br><br> 
<input type="submit" value="Submit"> 
</form> 

則動作:

<?php 
// Server credentials 
$vst_hostname = 'MYSERVERADDRESS'; 
$vst_username = 'MYSERVERUSERNAME'; 
$vst_password = 'MYSERVERPASSWORD'; 
$vst_command = 'v-add-user'; 

// New Account 
$username = 'username'; 
$password = 'password'; 
$email = 'email'; 
$package = 'Free'; 
$fist_name = 'Null'; 
$last_name = 'Null'; 

// Prepare POST query 
$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, 'http://' . $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"; 
} 
?> 

我認爲這個問題是輸入的表單數據到PHP腳本,但我不是很有經驗,所以我不知道。

的API連接到我的服務器作爲正確我測試相同

//Server Credentials 

部分具有不同的API,它的工作。

當我運行此腳本,輸出「用戶帳戶已成功創建」但是當我檢查我的控制面板,有沒有新的用戶。

謝謝你的幫助。

編輯:我只寫形式,這個腳本是由VestaCP作出如下所示:https://vestacp.com/docs/api/。這個腳本中的錯誤直接來自Vesta。

+1

不應該'$ =用戶名 '用戶名';'是'$用戶名= $ _ POST [ '用戶名'];'?和'$ fistname'? – frz3993

+0

我會建議你過時的腳本。你正在做的事情你不需要做或者你做了兩次。仔細檢查一下,就行了。 –

+0

@ frz3993我沒有寫這個劇本,它是從VestaCP這裏看到https://vestacp.com/docs/api/錯誤是寫的創建者。 –

回答

1

試試這個:

<form action="process.php" method="post"> 
Username:<br> 
<input type="text" name="username"> 
<br> 
Firstname:<br> 
<input type="text" name="firstname"> 
<br> 
Name:<br> 
<input type="text" name="name"> 
<br> 
Password:<br> 
<input type="password" name="password"> 
<br> 
Email Address:<br> 
<input type="email" name="email"> 
<br><br> 
<input type="submit" value="Submit"> 
</form> 

process.php

<?php 
// Server credentials 
$vst_hostname = 'MYSERVERADDRESS'; 
$vst_username = 'admin'; 
$vst_password = 'ADMINPASSWORD'; 
$vst_returncode = 'yes'; 
$vst_command = 'v-add-user'; 

// New Account 
$username = $_POST['username']; 
$password = $_POST['password']; 
$email = $_POST['email']; 
$package = 'default'; // $package = 'Free'; //Free package exist on server? 
$fist_name = $_POST['firstname']; 
$last_name = $_POST['name']; 

// Prepare POST query 
$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, 'http://' . $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"; 
} 
?> 
+0

謝謝,我會試試這個,如果這有效或不可行,請回復您。 –