如何在POST中使用计算出的变量并解析返回的绘图数组?

时间:2019-07-07 02:51:45

标签: php arrays json plot

需要帮助!!

上下文:我正在使用PHP从API端点检索数组(JSON),然后随着时间的推移使用IoT车辆传感器的结果值(时间,温度,湿度)填充图形。

简而言之:

问题1)构造主体,而不使用常量,而是使用curl为POST函数使用计算出的变量(如果可能)

问题2)解析JSON数组(时间,温度,湿度)中返回的值以在图形上绘图

问题1)我找到了一种成功构造并传递主体以获取JSON响应中的传感器数据的方法,但是仅通过使用常量-而不是使用计算变量就不理想,因为此处的目的是获取最后一个每当我运行脚本时,就会有24小时的数据。

问题2)我试图使foreach函数正常工作,但是我正在努力..如果可以使用计算出的变量,则应该只有24组数据。现在坐着很多,这意味着我需要做一个递归循环。

这些是我需要使用的计算值,但它们在班级中不起作用:

$time = $_SERVER['REQUEST_TIME'];
$startMs = (($time-18000)*1000);
$endMs = ($startMs-86400000);
$stepMs = 3600000;
$groupId = 34141;
$widgetId = 212014918549723;
$fillMissing = "withNull";
$field1 = "humidity";
$field2 = "temperature";
$endPoint = "https://api.samsara.com/v1/sensors/history?access_token=87ieoywNNcjdSbvgmhaprq3ffEPYsa";

class A
{ 
  public $endMs = 1561697999000; 
  public $fillMissing = 'withNull'; 
  public $groupId = 34141;
  public $startMs = 1561352400000;
  public $stepMs = 3600000;

  public $series = array(); 

function  __construct(){ 
      for ( $i=1; $i-->0;){ 
          array_push($this->series, new B);
          array_push($this->series, new C);
      } 
  } 
} 

class B { 
  public $field = "ambientTemperature";
  public $widgetId = 212014918549723;
} 

class C { 
  public $field = "humidity";
  public $widgetId = 212014918549723;
}                                                                      

$ch = curl_init('https://api.samsara.com/v1/sensors/history?access_token=87ieoywNNcjdSbvgmhaprq3ffEPYsa');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

$json = json_decode($result, true);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;

} else {
    echo "Values returned from the API:";
    print_r($json);
}

这是我当前的输出...

Array ( [results] => Array ( [0] => Array ( [timeMs] => 1561352400000 
[series] => Array ( [0] => 29105 [1] => 66 ) ) [1] => Array ( [timeMs] => 
1561356000000 [series] => Array ( [0] => 29469 [1] => 65 ) ) [2] => Array 
( [timeMs] => 1561359600000 [series] => Array ( [0] => 29115 [1] => 65 ) 
) [3] => Array ( [timeMs] => 1561363200000 [series] => Array ( [0] => 
28912 [1] => 65 ) ) [4] => Array ( [timeMs] => 1561366800000 [series] => 
Array ( [0] => 28740 [1] => 66 ) ) [5] => Array ( [timeMs] => 
1561370400000 [series] => Array ( [0] => 26895 [1] => 66 )......

0 个答案:

没有答案