简体   繁体   English

php变量怎么放

[英]How put php variable

I have this function:我有这个 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.'我想在第 8 行添加可变日期,并且我尝试过{$date} $date '.$date.' ".$date." every time I get error from api每次我从 api 收到错误

If I put value manually like 1589972726 it working, but I need to get value from $date variable!如果我像 1589972726 这样手动输入值,它可以工作,但我需要从 $date 变量中获取值!

Thank you!谢谢!

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_encode for details:有关详细信息,请查看json_encode的文档:

https://www.php.net/manual/function.json-encode.php 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
}';

Because you have single quote on that string, you need to inject with single quote: '.$date.'因为你在那个字符串上有单引号,你需要用单引号注入: '.$date.' , ,

if you would have double quote you use {$date} or \"$date\"如果你有双引号,你使用{$date}\"$date\"

$methodParams = '{
                "date_confirmed_from": '.$date.',
                "get_unconfirmed_orders": false
            }';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM