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);
Related
I'm trying to validate api data with POST request using cURL but getting no response.
API documentation
<?php
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$data = array(
"Parameters" => array(
"apiKey" => "XXXXXX",
"id" => "9346",
)
);
$encoded = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$decoded = json_decode($resp);
print_r($decoded);
curl_close($ch);
?>
Does anyone know what is wrong?
SOLUTION:
Turns out i was missing CURL_HTTPHEADER.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json"
));
Try to write:
$ch = curl_init();
instead of :
$ch = curl_init($url);
Eventualy you can use a try ... catch to get the error:
<?php
// Define variables
define('API_KEY', 'XXXXXX');
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$id = "9346";
// Prepare data
$data = array(
"Parameters" => array(
"apiKey" => API_KEY,
"id" => $id,
)
);
$encoded = json_encode($data);
try {
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$resp = curl_exec($ch);
if($resp === false) {
throw new Exception(curl_error($ch));
}
// Decode response and print it
$decoded = json_decode($resp);
print_r($decoded);
// Close cURL session
curl_close($ch);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
I'm trying to send string variable to php file with post method this is my function in dart:
Future<Verify> verifyOTP() async {
var response_verifyotp = await http.post(Uri.parse(linkverifyOTP), body: {
"OTP_code": SOTP,
});
if (response_verifyotp.statusCode == 200) {
print(response_verifyotp.body);
st_verfiy = Verify.fromJson(json.decode(response_verifyotp.body));
print(st_verfiy.status);
}
}
but it doesn't pass correctly still appear to me it's missing value
I'm try also with: "OTP_code": SOTP.toString(),
it's same
This is my php code:
<?php
session_start();
$ch = curl_init();
$OTP_code=$_POST["OTP_code"];
if(isset($_SESSION["id"]))
$id=$_SESSION["id"];
curl_setopt($ch, CURLOPT_URL, "https://www.msegat.com/gw/verifyOTPCode.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
$fields = <<<EOT
{
"lang":"EN",
"userName": "Bloom_Ducks",
"apiKey":"f92b62af08ed0be",
"code":"$OTP_code",
"id": "$id" ,
"userSender":"APP"
}
EOT;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$response=json_decode($response,true);
$code=$response["code"];
if($code == 1){
json_encode(array("status" => 1));
}else{
json_encode(array("status" => 0));
}
?>
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'];
}
I am using this function for calling method from one server to another in PHP.
function get_url($request_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
}
$request_url = 'http://second-server-address/listening_page.php?function=somefunction';
$response = get_url($request_url);
Here I am giving a URL with function name. The question is what if the function receives few parameters? How would we pass parameters to the method on another server using CURL.
Just add
$request_url = 'http://second-server-address/listening_page.php?function=somefunction&funcParam1=val&funcParam2.val
Use these passed parameters in your function
If you want to pass parameter as post request then try this.
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
}
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
post_to_url("http://yoursite.com/post-to-page.php", $data);
I can not send pictures.
More information - Telegram
https://core.telegram.org/bots/api#sendphoto
I want to send a picture, this method is useless.
Are there any other method?
<?php
$Photo = "http://www.pawprint.net/images/news/1-4fac83467069c.png";
$IDUser = 26034352;
$Data = array(
'chat_id' => $IDUser,
'photo' => $Photo,
'caption' => 'hi'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/bot93816942:AAHnjqsjpJjRItc7ySbUq4C5IRLqytpPK6k/sendPhoto");
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS,$options);
// in real life you should use something like:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query($Data));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// frther processing ....
if ($server_output == "OK") {
echo "ok";
}
?>
I see you want to send a picture located on a external server with its URL
so you can do as follow
define('website', 'https://api.telegram.org/bot<bot-token>');
function send($method, $datas)
{
$url = website . "/" . $method;
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $datas);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
}
now just call this function with proper method name and data. example for sending a photo:
$postfields = array(
'chat_id' => $ChatID,
'photo' => "http://url.of/photo.jpg"
);
send("sendPhoto", $postfields);