json PHP每24小时存储一个动态变量的结果

zujrkrfu  于 4个月前  发布在  PHP
关注(0)|答案(1)|浏览(50)

我找了很长时间,但没有找到这个地方。所以让我们说,我做了一个API调用,并把一些数据在一个变量。

<?php
$coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD'), true); 
        
    
    $testVar = $coinData['RAW']['BTC']['USD']['PRICE'];

    
    echo $testVar;
     
   ?>

字符串
现在我在$testVar变量中得到了比特币价格的实际实时值。这个值一直在变化。
我怎样才能每24小时对这个变量做一个“快照”,并存储这个数据值?我找不到任何关于如何从动态变量中“快照”值的信息。
例如,在1小时内,我想从变量$testVar中获取“快照”,此时值为5322.15。现在我想将此精确值自动存储为5322.15,以便稍后使用。

rkue9o1l

rkue9o1l1#

将cron作业设置为每24小时运行一次,然后将每天的值写入数据库表中的新行。例如,沿着以下行:

id int NOT NULL,
timestamp datetime NOT NULL,
current_value decimal(10,2) NOT NULL,

字符串
你的cron job看起来像这样:

<?php
$db= mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);//connect to db
$coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD'), true); 
$current_value = $coinData['RAW']['BTC']['USD']['PRICE'];
$timestamp = date('Y-m-d H:i:s');
$query = $db->prepare("INSERT INTO currency_values (current_value, timestamp) VALUES (?, ?)");
$query->execute([$current_value, $timestamp]);

相关问题