VESTA, API, PHP. No user is added - php

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.

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";
}
?>

Related

PHP cURL POST Request won't send?

I'm trying to update a mySQL database with cURL and PHP, but the values that are supposed to be sent by the post aren't inserted into the database. I don't get any errors and cURL is enabled according to phpinfo(). Here is my script:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
And here's the receiver script:
<?php
require 'includes/db.php'; // Database connection. Connects with no errors raised
if(isset($_POST['email']) AND isset($_POST['token'])) {
$email = $_POST['email'];
$token = $_POST['token'];
$q = "INSERT INTO reset_tokens(email, token)";
$q .= " VALUES(?, ?)";
$stmt = $pdo->prepare($q);
$test = $stmt->execute([$email, $token]);
}
?>
Looks like you are missing HTTP Auth for http://www.econcentre.com/receiver.php.
You should include the HTTP Auth in the cURL request:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$username = "auth-user";
$password = "auth-pwd";
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);
?>

Return value from function where called

In the following custom function...
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
echo $balance['balance']['sms'];
}
I return a balance from a text local account. Now i try and echo this balance into place...
$balance = get_txtlocal_balance();
$title = 'SMS Integration - Current Balance: ' . $balance;
<span><?php echo $title;?></span>
But the returned value doesnt appear in the right place, it always returns are the top of the page, can anyone spot what im doing wrong?
Replace Below Code :
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
return $balance['balance']['sms'];
}

How can i set the title of my push notification in my php script?

This is my php script and I am sending the body and the title strings from my android app .Here I'm able to get the message body in my notification but not the title and even if I put;
$message = array(
'message' => $body,
'title' => 'This is my title',
'sound' => 1
);
This is also not working.
<?php
$body = $_POST["body"];
$title = $_POST["title"];
$inc_phone = $_POST["inc_phone"];
function send_notification ($tokens,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = AIzaSyAe9Qxx2URTyqXmf0mTheMq_ss_DOJaVQs ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
//Connect to the database
$host = "127.0.0.1";
$user = "rajhanssingh"; //Your Cloud 9 username
$pass = ""; //Remember, there is NO password by default!
$db = "fcm"; //Your database name you want to connect to
$port = 3306; //The port #. It is always 3306
$conn = mysqli_connect($host, $user, $pass, $db, $port);
//$conn = mysqli_connect("localhost","root","","fcm");
$sql = " Select token From utoken where phone_no='$inc_phone' ";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["token"];
}
}
mysqli_close($conn);
$message = array(
'message' => $body,
'title' => $title,
'sound' => 1
);
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
this worked for me
function send_push_notification($registrationIDs, $message) {
$GOOGLE_API_KEY = "AIzaSyCzr6K_W3ImYwVQ-vQyhFztDSmchkFjeGQ";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => ['message' => $message, 'title' => 'Title' ]
);
$headers = array(
'Authorization: key=' . $GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// echo $result;
}

foreach loop working only for first record of database in php

I am stuck in one condition in my code. I am sending gcm push notifications to multiple user from php. I am getting the reg id's from the database. In code I am using foreach loop to send notification. But I am not sure why it is sending the push notification to only one user not to the all user. if I remove the push notification code then it will show me all the reg ids from the database.
<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
echo"could not connect:".mysql_error();
} else {
mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
$gcmRegID = $r_id[0];
echo $gcmRegID . "<br>";
$pushMessage = "New Property Posted in CPO";
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcmRegIds,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}
?>
In short foreach loop is sending the notification to the first user only not to all. If I remove the gcm code and do echo of foreach loop records its show me all
Thanks
You have a return in your foreach loop, this will cause the first pass through the foreach to fire the retrun call and never loop through another foreach. The return is not needed. Use the following code instead.
<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
echo"could not connect:".mysql_error();
} else {
mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
$gcmRegID = $r_id[0];
echo $gcmRegID . "<br>";
$pushMessage = "New Property Posted in CPO";
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcmRegIds,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
}
?>

Curl Redirect Issue

0I'm trying to redirect the user to a 'Thank You' page on success, I'm using a simple php redirect.
header('Location: http://client.com/thank-you-one.php');
and I've added it into the snippet below but the redirect still isn't working, if anyone know why or can see where I'm going wrong I'd love some feedback.
$username = '*****';
$password = '*****';
//POPULATE THESE (and other fields) WITH REQUEST DATA
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['emailaddress'];
$company_name = $_POST['companyname'];
$date_of_birth = (''.$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'].'');
$loyalty_number = $_POST['membernumber'];
$host = 'http://api.client.com/users';
$user_curl = curl_init($host);
curl_setopt($user_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($user_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($user_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($user_curl, CURLOPT_POST, 1);
curl_setopt($user_curl, CURLOPT_POSTFIELDS, http_build_query(array('forename' = > $first_name, 'surname' = > $last_name, 'email' = > $email, 'company_name' = > $company_name, 'date_of_birth' = > $date_of_birth, 'loyalty_number' = > $loyalty_number)));
curl_setopt($user_curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($user_curl);
$info = curl_getinfo($user_curl);
if ($info['http_code'] == '200') {
$data = json_decode($return, true);
$tag_curl = curl_init($host.'/'.$data['id'].'/tags/'.$_POST['favorite'].'');
curl_setopt($tag_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($tag_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($tag_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($tag_curl, CURLOPT_POST, 1);
curl_setopt($tag_curl, CURLOPT_RETURNTRANSFER, TRUE);
$tag_return = curl_exec($tag_curl);
$ml_curl = curl_init($host.'/'.$data['id'].'/mailinglists/3');
curl_setopt($ml_curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.aardvark.crm+json; version=1.3'));
curl_setopt($ml_curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ml_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($ml_curl, CURLOPT_PUT, 1);
curl_setopt($ml_curl, CURLOPT_RETURNTRANSFER, TRUE);
$tag_return = curl_exec($ml_curl);
header('Location: http://client.com/thank-you-one.php');
echo 'test1';
exit;
} else {
//analyse resonse with
$data = json_decode($return, true);
print_r($data);
echo 'test2';
}
curl_close($user_curl);
curl_close($tag_curl);
curl_close($ml_curl);
I believe your issue is your testing for http_code 201. change it to 200
if ($info['http_code'] == '200') {

Categories