Someone could help me ? I'm trying to do a request by the code bellow, but anything happen, any message appears. I believe my code it's right:
public function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
));
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch); // Getting jSON result string
print_r($result);
}
Find link with instructions of the site:
you will reiceive this:
[
{
"Code": 114,
"Message": "The provided MerchantId is not in correct format"
}
]
with this code:
function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(
array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
)
);
$headers = array(
'Content-Type: application/json',
'MerchantId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'MerchantKey: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'RequestId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx'
);
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options ); $result = curl_exec($ch);
print_r($result);
}
subscribe()
It would be interesting what HTTP status code you get:
print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE));
Related
Long story short, i have the following code which is gettin info about an order from api in json, and should send this order to another system through api. All steps works good, but my problem it's if i have more products on order, my script it's creating a new order for each one.
SCRIPT:
foreach($data['orders'] as $key => $val)
{
$phone = $val['phone'];
$email = $val['email'];
$fullname = $val['invoice_fullname'];
$invoicecompany = $val['invoice_company'];
$invoicenip = $val['invoice_nip'];
$invoiceaddress = $val['invoice_address'];
$invoicecity = $val['invoice_city'];
$deliveryprice = $val['delivery_price'];
$deliverymethod = $val['delivery_method'];
}
foreach($data['orders'][0]['products'] as $key => $val){
$pidBl = $val['product_id'];
$ean = $val['ean'];
$grossprice = $val['price_brutto'];
$vatrate = $val['tax_rate'];
$quantity = $val['quantity'];
//open db
$dbc = mysqli_connect ($dbhost, $dbuser, $dbpassword);
$dbSelected = mysqli_select_db($dbc , $database);
$q = $dbc->query("SELECT pId FROM offers WHERE pBarcode = '$ean';");
while ($rs = $q->fetch_assoc()) {
$pId = $rs['pId'];
}
$arr = [
"objects" => [],
"returnColumns" => []
];
$arr['objects'][] = [
"name" => $fullname,
"uniqueCode" => $invoicenip,
"active" => "true"
];
$arr['returnColumns'][] = ["name" => "id"];
$preparesedona = json_encode($arr);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'BLABLABLA',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $preparesedona,
CURLOPT_HTTPHEADER => array(
'accept: application/json',
'Authorization: Basic BLABLABLA',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
foreach($data['result'] as $key => $val){
$customerId = $val['id'];
}
$arr = [
"objects" => [],
"returnColumns" => []
];
$arr['objects'][] = [
"client_id" => $customerId,
"user_id" => 1,
"administration_id" => 1,
"status" => 1,
"deliveryType" => 1,
"details" => []
];
$arr['objects'][0]['details'][] = [
"product_id" => $pId,
"unitPriceWithVat" => $grossprice,
"orderedUnits" => $quantity
];
$arr['returnColumns'][] = ["name" => "id"];
$preparesedona = json_encode($arr);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'BLABLABLA',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $preparesedona,
CURLOPT_HTTPHEADER => array(
'accept: application/json',
'Authorization: Basic BLABLABLA',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
Yas, i know that somewhere it's a small thing which is making difference, but trust me, i tried in any possible way, i tried to look on similar errors and problems but i cannot find anything related cause i have multiple arrays which should be defined and filled...
This is how it looks my postfields
{
"objects":[
{
"client_id":21947,
"user_id":1,
"administration_id":1,
"status":1,
"deliveryType":1,
"details":[
{
"product_id":"11407",
"unitPriceWithVat":159.7,
"orderedUnits":1
}
]
}
],
"returnColumns":[
{
"name":"id"
}
]
}{
"objects":[
{
"client_id":21948,
"user_id":1,
"administration_id":1,
"status":1,
"deliveryType":1,
"details":[
{
"product_id":"14575",
"unitPriceWithVat":45.31,
"orderedUnits":1
}
]
}
],
"returnColumns":[
{
"name":"id"
}
]
}
This is how should look like:
{
"objects":[
{
"client_id":21947,
"user_id":1,
"administration_id":1,
"status":1,
"deliveryType":1,
"details":[
{
"product_id":"11407",
"unitPriceWithVat":159.7,
"orderedUnits":1
},
{
"product_id":"14575",
"unitPriceWithVat":45.31,
"orderedUnits":1
}
]
}
],
"returnColumns":[
{
"name":"id"
}
]
}
I would much appreciate your help and please, be sympathetic, i'm not a pro, i just try to learn and i promise that i do my best :)
this works for me, baiscally, i make one call to API1 to get general info about order and creating customer in API2 // after that i made another call to API1 to get info about products and this is how i structured my script and it's workin very well.
// gettin info about order data
foreach($data['orders'] as $key => $val)
{
$phone = $val['phone'];
$email = $val['email'];
$fullname = $val['invoice_fullname'];
$invoicecompany = $val['invoice_company'];
$invoicenip = $val['invoice_nip'];
$invoiceaddress = $val['invoice_address'];
$invoicecity = $val['invoice_city'];
$deliveryprice = $val['delivery_price'];
$deliverymethod = $val['delivery_method'];
$deliveryaddress = $val ['delivery_address'];
// CREATE CUSTOMER -------------------------
$arr = [
"objects" => [],
"returnColumns" => []
];
$arr['objects'][] = [
"name" => $fullname,
"uniqueCode" => $invoicenip,
"active" => "true"
];
$arr['returnColumns'][] = ["name" => "id"];
$preparesedona = json_encode($arr);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'BLABLABLABLA',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $preparesedona,
CURLOPT_HTTPHEADER => array(
'accept: application/json',
'Authorization: Basic BLABLABLABLA',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
$customerId = $data['result'][0]['id'];
}
// Call again to get data for products
$methodParams = [
'order_id' => $orderidbl
];
$apiParams = [
'method' => 'getOrders',
'parameters' => json_encode($methodParams),
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'BLABLABLABLABLABLABLABLA');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-BLToken: BLABLABLABLA-BLABLABLABLA-BLABLABLABLA"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));
$output = curl_exec($ch);
curl_close($ch);
$data = json_decode($output, true);
$arr = [
"objects" => [],
"returnColumns" => []
];
$arr['objects'][] = [
"client_id" => $customerId,
"user_id" => 1,
"administration_id" => 1,
"status" => 1,
"deliveryType" => 1,
"deliveryAddress" => $deliveryaddress,
"details" => []
];
$arr['returnColumns'][] = ["name" => "id"];
// prepare products area "details"
foreach($data['orders'][0]['products'] as $key => $val){
$pidBl = $val['product_id'];
$ean = $val['ean'];
$grossprice = $val['price_brutto'];
$vatrate = $val['tax_rate'];
$quantity = $val['quantity'];
//open db
$dbc = mysqli_connect ($dbhost, $dbuser, $dbpassword);
$dbSelected = mysqli_select_db($dbc , $database);
$q = $dbc->query("SELECT pId FROM offers WHERE pBarcode = '$ean';");
while ($rs = $q->fetch_assoc()) {
$pId = $rs['pId'];
}
$arr['objects'][0]['details'][] = [
"product_id" => $pId,
"unitPriceWithVat" => $grossprice,
"orderedUnits" => $quantity
];
}
$preparesedona = json_encode($arr);
var_dump($preparesedona);
I've created 2 Inline-Button and I want to evaluate the callback_data. But unfortunately, the callback_data is not sent.
<?php
$bot_token = 'There is no Token seen'; // Telegram bot token
$url = "https://api.telegram.org/bot$bot_token/sendMessage";
$content = file_get_contents('php://input');
$update = json_decode($content, TRUE);
$callback_query = $update['callbackQuery'];
$callback_data = $callback_query['data'];
$ser_update = serialize($update);
db_query("INSERT INTO prefix_telegram (text) VALUES ('".$ser_update."')");
if (isset($update['message']['text'])) {
$text = $update['message']['text'];
$chat_id = $update['message']['chat']['id'];
if (strpos($text, 'outi') !== false) {
$reply = utf8_encode("Wähle einen Button!");
$keyboard = array(
"keyboard" => array(
array(
array(
"text" => "Button1",
"callback_data" => "1",
),
array(
"text" => "Button2",
"callback_data" => "2",
),
)
),
"one_time_keyboard" => true,
"resize_keyboard" => true
);
$postfields = array(
'chat_id' => "$chat_id",
'text' => "$reply",
'reply_markup' => json_encode($keyboard)
);
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curld, CURLOPT_URL,$url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close ($curld);
}
}
Result of file_get_contents
Could anyone help me with this problem? I have sent more information than just the text of the clicked button. Thanks!
There is muuuuch to correct.
In line 7, it is $callback_query = $update['callback_query'];
From line 19 to 34:
$keyboard = array(
"inline_keyboard" => array( // inline_keyboard not keyboard
array(
array(
"text" => "Button1",
"callback_data" => "1",
),
array(
"text" => "Button2",
"callback_data" => "2",
),
)
) //Removed onetimekeyboard etc.
);
From 36 to 40:
$postfields = array(
'chat_id' => $chat_id, // you don't have to use quotes when you're using variables or numbers.
'text' => $reply,
'reply_markup' => json_encode($keyboard)
);
The callback_data is not supported for reply_markup.keyboard. It is supported only for reply_markup.inline_keyboard.
You can vote for the issue Add callback_data to KeyboardButton (reply_markup.keyboard).
I have two array of objects that I want to post to an API but it displays an error missing value for parameter: productDC
it seems that i have to pass ProductDc and buyerDC along with their data. for this I also tried
$data_string = array({ProductDc"product_name" => "Electronics","price" => "200 usd"},
buyerDC{"buyer_name" => "john mark","address" => "17 more strret"});
below is the informations
1.) ProductDC
pass it as array of objects
**product_name(string)
price (string)**
2.) buyerDC
pass it as array of objects
**buyer_name(string)
address (string**)
here is my code
<?php
$data_string = array("product_name" => "Electronics","price" => "200 usd",
"buyer_name" => "john mark","address" => "17 more strret");
$data = json_encode($data_string);
//$data = $data_string;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "myapi.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"content-type: application/json; charset=utf-8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Considering below information that you have provided if they want a data in the format in which -
1.) ProductDC should be an array of objects containing -
product_name(string)
price (string)
2.) buyerDC should be array of objects containing-
buyer_name(string)
address (string)
Probably they are expecting the data in a different form which you may get by changing $data_string value as below -
$data_string = array(
"ProductDc"=>array(
"product_name" => "Electronics",
"price" => "200 usd"
),
"buyerDC"=>array(
"buyer_name" => "john mark",
"address" => "17 more strret"
)
);
Change your $data_string value to the one I posted above and check.
I'm trying to get latitude and longitude from cell tower info using Google's geolocation api.
It requires a valid JSON with information like MCC, MNC, cellId, lac etc.., My PHP post request looks like this.
<?php
header("Access-Control-Allow-Origin: *");
$mcc = $_POST["mcc"];
$mnc = $_POST["mnc"];
$cellId = $_POST["cellId"];
$lac = $_POST["lac"];
$post_array = array(
"cellId" => (int) $cellId,
"locationAreaCode" => (int) $lac,
"mobileCountryCode" => (int) $mcc,
"mobileNetworkCode" => (int) $mnc,
);
$post_data = json_encode(array('cellTowers' => array($post_array)));
echo $post_data;
$url = "https://www.googleapis.com/geolocation/v1/geolocate?key=".$api_key; // not including api key here but its there in my code
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => $post_data
));
$result = curl_exec($ch);
echo "Result: ".$result;
curl_close($ch);
?>
However I get an error saying bad request in the response. The error is shown below.
Result: {
"error": {
"errors": [
{
"domain": "geolocation",
"reason": "invalidRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
I thought my JSON was not in the correct format but it was working with the following command line execution, so that can't be the issue.
$ curl -d #your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY"
The above command in terminal gives lattitude and longitude properly with the same JSON in a file. What am I doing wrong ?
Try this
$DadosLBS['homeMobileCountryCode'] = $data['MCC'];
$DadosLBS['homeMobileNetworkCode'] = $data['MNC'];
$DadosLBS['radioType'] = 'gsm';
$DadosLBS['carrier'] = $data['MNCOperator'];
$DadosLBS['cellTowers'] = [
[
'mobileCountryCode' => $data['MCC'],
'mobileNetworkCode' => $data['MNC'],
'age' => $data['Age'],
'timingAdvance' => $data['TA'],
'locationAreaCode' => $data['LAC'],
'cellId' => $data['CELL_ID'],
'signalStrength' => $data['SIGNAL'],
],
];
//Ver detalhes da API no https://developers.google.com/maps/documentation/geolocation/intro?hl=pt-br
$service_url = "https://www.googleapis.com/geolocation/v1/geolocate";
//Chave de acesso
$Curl_Data = array(
'key' => <YOUR KEY HERE>
);
$CurlQueryString = http_build_query($Curl_Data);
//Preparando o método a ser enviado os dados
$Metodo = array(
CURLOPT_URL => $service_url.'?'.$CurlQueryString // Define URL to be called
);
//Criando s string de dados
$DadosPost = json_encode($DadosLBS);
//Preparando as opções padrões do CUrl
$Curl_Adicional_Options = array(
CURLOPT_CUSTOMREQUEST => "POST"
,CURLOPT_POSTFIELDS => $DadosPost
,CURLOPT_RETURNTRANSFER => true // return web page
,CURLOPT_CONNECTTIMEOUT => 15 // time-out on connect
,CURLOPT_TIMEOUT => 15 // time-out on response
,CURLOPT_FAILONERROR => true //
,CURLOPT_HEADER => false // don't return headers
,CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($DadosPost)
) // Dados para o cabeçalho do post
,CURLOPT_FOLLOWLOCATION => true // follow redirects
,CURLOPT_MAXREDIRS => 10 // stop after 10 redirects
,CURLOPT_SSL_VERIFYPEER => false
,CURLOPT_SSL_VERIFYHOST => false
);
$Curl_Options = array_replace_recursive($Metodo,$Curl_Adicional_Options);
$cURLConn = curl_init();
curl_setopt_array($cURLConn, $Curl_Options);
$vDados['Curl']['Output'] = curl_exec($cURLConn);
$vDados['Curl']['Error'] = curl_error($cURLConn);
$vDados['Curl']['ErrorNum'] = curl_errno($cURLConn);
$vDados['Curl']['ErrorMsg'] = curl_strerror($vDados['Curl']['ErrorNum']);
$vDados['Curl']['Info'] = curl_getinfo($cURLConn);
curl_close($cURLConn);
if ($vDados['Curl']['ErrorNum'] != 0) {
$Dados['loc'] = array(
'status' => 'ERROR',
'error' => array(
'error_cod' => $vDados['Curl']['ErrorNum'],
'error_msg' => $vDados['Curl']['ErrorMsg']
)
);
return $Dados['loc'];
}
//Tratando as respostas
$vDados['Curl']['Dados'] = json_decode($vDados['Curl']['Output']) or die("Error: Cannot create object");
print_r($vDados['Curl']['Dados']);
Don't forget to create our key on google console.
So I'm following the Pushover FAQ example for PHP:
<?php
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "APP_TOKEN",
"user" => "USER_KEY",
"message" => "hello world",
)));
curl_exec($ch);
curl_close($ch);
?>
That example works great, but if I try to send the message as a variable like:
"message" => $variable,
It gives me an error saying that I can't send a blank message.
I guess it's a language related problem. How can I assign a variable to the array "message"?
Thank you.
Maybe there is a problem with Curl, you can use this function to post array data into api.pushover.
function sendApiPushover(){
$url = 'https://api.pushover.net/1/messages.json';
$data = array(
"token" => "APP_TOKEN",
"user" => "USER_KEY",
"title" => "John",
"message" => "hello world"
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
It seems that your variable $message is empty. Better to check before running this script by:
<?php
if(!empty($message)){
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "APP_TOKEN",
"user" => "USER_KEY",
"message" => $message,
)));
curl_exec($ch);
curl_close($ch);
}
?>