I added $amount it started showing null
I added $amount it started showing null
I added $amount it started showing null
I added $amount it started showing null
<?php
$api = $_GET['api'];
$amount = $_GET['amount'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.googl.com',
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 =>'{
"price_amount": $amount,
"price_currency": "usd",
"pay_currency": "btc"
}',
CURLOPT_HTTPHEADER => array(
'x-api-key:' . $api,
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
$json2 = json_encode(json_decode($response), JSON_PRETTY_PRINT);
echo $json2;
?>
CodePudding user response:
When you say "I added $amount it started showing null", could you please clarify? What exactly is showing null (what is "it")? More details are needed, but I can see one issue right off the bat--the following isn't going to work:
CURLOPT_POSTFIELDS =>'{
"price_amount": $amount,
"price_currency": "usd",
"pay_currency": "btc"
}',
Strings created with single quotes (') do not parse PHP variables, only strings created with double quotes (") will parse variables. So in the example provided, the $amount variable there is just going to be the literal string "$amount", and not the value contained by the variable. However I would strongly urge you to not try and create this JSON string manually like this anyhow, instead create a PHP object/array that you want and use the json_encode() function to create the JSON string from that object.
Something like:
$myJsonObj = array(
'price_amount' => $amount,
'price_currency' => 'usd',
'pay_currency' => 'btc'
);
And then:
CURLOPT_POSTFIELDS => json_encode($myJsonObj),