Return value from function where called - php

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'];
}

Related

how to send post data using curl in php array json

how to send post data using curl in php array json
post curl param orderlist like an array
this is my code
<?php
class PostOrderPolicy extends CI_Controller{
public function index(){
$data = $this->getData();
foreach ($data as $key) {
$orderListt = array(
'orderDate' => $key['order_date'],
'IDNumber' => $key['IDnumber'],
'itemDescription' => $key['description']
);
$send = $this->sendData($orderListt, $key['resi_number']);
if ($send) {
$res["message"] = "Sukses";
}else{
$res["message"] = "Gagal";
}
}
//echo json_encode($res);
}
private function getData(){
$sql = "SELECT * FROM t_transaction WHERE status='00'";
return $this->db->query($sql)->result_array();
}
private function sendData($data){
$result = array();
$url = "https://contoh.co.id/createPolicy";
$res = array();
$sign = $this->getSign();
$upperSign = strtoupper($sign);
//$random = $this->getRandomStr();
$random = '049KN1PSOL16QHIF';
$batch = 'BATCH000002';
$dataArr = array(
'sign' => $upperSign,
'randomStr' => $random,
'batchNo' => $batch,
'orderList' => [$data]
);
$string = json_encode($dataArr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$response = json_decode(curl_exec($ch), TRUE);
print_r($response);
print_r($string);
}
}
?>
but my result post field
[enter image description here][1]
[1]: https://i.stack.imgur.com/b7LEP.png
i want like this
[enter image description here][2]
[2]: https://i.stack.imgur.com/xExJ2.png
{"sign":"35EAF78F95A52DDA79FC9911559026F6","randomStr":"049KN1PSOL16QHIF","batchNo":"BATCH000002","orderList":[{"orderDate":"2020-07-01 11:00:00.000","resiNumber":"99875646957","itemDescription":"testing"},{"orderDate":"2020-07-01 ]11:00:00.000","IDNumber":"28929740170","itemDescription":"testing"}]}
Return response instead of outputting:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Execute the POST request:
$result = curl_exec($ch);
Close cURL resource:
curl_close($ch);

VESTA, API, PHP. No user is added

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

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);
?>

Bitaps.com Error Code 10

This is the exact code they give me in the Query examples with the fields i added. No matter what I cannot get it to work. There other examples work but not the 1 i need.
The error I get: {"error_code": 10, "message": "payment_list element incorrect keys", "details": ""}
However it works when I use their onsite execute program.
<?php
function post_api($url, $postfields) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
return $result;
}
$confirmations = 3;
$callback = urlencode("https://website.ml/ipnbtc?id=2&email=email#gmail.com");
$fee = "low";
$rules = [
array('address'=>'btc1', 'qouta'=> 95),
array('address'=>'btc2', 'qouta'=> 5),
array('address'=>'btc2', 'qouta'=> 0)
];
$postfields = json_encode(array('type'=>"payment_distribution", 'payment_distribution'=> $rules ));
$data = post_api("https://bitaps.com/api/create/payment/smartcontract/". $callback . "?confirmations=" . $confirmations . "&fee_level=" . $fee, $postfields);
echo $data;
$respond = json_decode($data,true);
$address = $respond["address"]; // Bitcoin address to receive payments
$payment_code = $respond["payment_code"]; //Payment Code
$invoice = $respond["invoice"]; // Invoice to view payments and transactions
?>
Mistake in word qouta. Need quota
$rules = [
array('address'=>'btc1', 'quota'=> 95),
array('address'=>'btc2', 'quota'=> 5),
array('address'=>'btc2', 'quota'=> 0)
];

Send header and post in curl to localbitcoins

I had tried to send info in a post and a header throw localbitcoins and get one error:
HMAC authentication key and signature was given, but they are invalid
From what i learn with localbitcoins api is this just a code u get when u mess with the header can someone help me to solve why I get this error because don't know whats wrong in my code:
function localbitcoins_query2($path, array $req = Array()) {
$key='mycode';
$secret='mycode';
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);
if ($req) {
$get=httpbuildquery($req);
$path=$path.'?'.$get;
}
$postdata=$nonce.$key.$path;
$sign = strtoupper(hash_hmac('sha256', $postdata, $secret));
$headers = array(
'Apiauth-Signature:'.$sign,
'Apiauth-Key:'.$key,
'Apiauth-Nonce:'.$nonce
);
$ch = null;
$ch = curl_init();
$data = array("lat" => "Hagrid", "price_equation" => "36");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_URL,"https://localbitcoins.com".$path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
$res = curl_exec($ch);
if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data: '.$res);
curl_close($ch);
return $dec;
}
Try this solution:
function localbitcoins_query2($path, array $req = Array()) {
$key='mycode';
$secret='mycode';
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);
$get = "";
if ($req) {
$get=httpbuildquery($req);
}
$postdata=$nonce.$key.$path.$get; // NOTE: $postdata without '?' char before the parameters!;
$sign = strtoupper(hash_hmac('sha256', $postdata, $secret));
$headers = array(
'Apiauth-Signature:'.$sign,
'Apiauth-Key:'.$key,
'Apiauth-Nonce:'.$nonce
);
$ch = null;
$ch = curl_init();
$data = array("lat" => "Hagrid", "price_equation" => "36");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_URL,"https://localbitcoins.com".$path.( $get=="" ? "" : "?".$get)); // NOTE: here it's necesary '?' char before the parameters!);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
$res = curl_exec($ch);
if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data: '.$res);
curl_close($ch);
return $dec;
}

Categories