'How put php variable
I have this function:
function api()
{
$date = time()- 86400;
$method = "getOrders";
$methodParams = '{
"date_confirmed_from": $date,
"get_unconfirmed_orders": false
}';
$apiParams = [
"method" => $method,
"parameters" => $methodParams
];
$curl = curl_init("https://api.domain.com");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken:xxx"]);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
$response = curl_exec($curl);
return $response;
}
I want to put on line 8 variable date and I have tried {$date} $date '.$date.' ".$date." every time I get error from api
If I put value manually like 1589972726 it working, but I need to get value from $date variable!
Thank you!
Solution 1:[1]
Maybe it is less confusing, if you use an array and convert it to the needed format later:
$methodParams = [
"date_confirmed_from" => $date,
"get_unconfirmed_orders" => false
];
$apiParams = [
"method" => $method,
"parameters" => json_encode($methodParams)
];
Have a look at the documentation of json_encodefor details:
https://www.php.net/manual/function.json-encode.php
Anyways, using the same quotes that started the string to close it, should work too:
$methodParams = '{
"date_confirmed_from": ' . $date . ',
"get_unconfirmed_orders": false
}';
Solution 2:[2]
Because you have single quote on that string, you need to inject with single quote: '.$date.',
if you would have double quote you use {$date} or \"$date\"
$methodParams = '{
"date_confirmed_from": '.$date.',
"get_unconfirmed_orders": false
}';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | t.h3ads |
| Solution 2 | BoBiTza |
