get.php 1.79 KiB
<?php
namespace SmartData;
require_once('../bin/smartdata/SmartAPI.php');
use function SmartData\SmartAPI\get;
use SmartData\Exception\CustomException;
use SmartData\Logger;
function series2csv($response) {
$str_header = "version,timestamp,unit,value,error,confidence,x,y,z,dev";
$str_data = "";
$json_resp = json_decode($response);
$series = $json_resp->series;
foreach ($series as &$smartdata) {
$str_header .= "\n{$smartdata->version},";
$str_header .= "{$smartdata->timestamp},";
$str_header .= "{$smartdata->unit},";
$str_header .= "{$smartdata->value},";
$str_header .= "{$smartdata->error},";
$str_header .= "{$smartdata->confidence},";
$str_header .= "{$smartdata->x},";
$str_header .= "{$smartdata->y},";
$str_header .= "{$smartdata->z},";
$str_header .= "{$smartdata->dev}";
}
unset($smartdata);
$resp = array('series' => $str_header);
return json_encode($resp);
}
http_response_code(HttpStatusCode::BAD_REQUEST);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Content-length, X-Requested-With');
header("Access-Control-Allow-Methods: POST");
try {
$content = file_get_contents('php://input');
$response = get($content);
if(isset($_GET['csv'])) {
$response = series2csv($response);
}
}catch(CustomException $e){
http_response_code($e->getHTTPCodeError());
Logger::exception($e);
return false;
}catch(\Exception $e){
http_response_code(HttpStatusCode::BAD_REQUEST);
Logger::exception($e);
return false;
}
http_response_code(HttpStatusCode::OK);
if ( isset( $response['headers'] ) ){
foreach ( $response['headers'] as $header )
header( $header );
} else {
echo $response;
}