62 lines
2.2 KiB
PHP
62 lines
2.2 KiB
PHP
<?php
|
|
include_once 'db_config.php';
|
|
//header('Content-Type: application/json');
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
|
|
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
|
|
|
if($_SERVER['REQUEST_METHOD'] == 'POST'){
|
|
|
|
// 获取 POST 数据
|
|
$postData = file_get_contents('php://input');
|
|
echo $postData;
|
|
// 解析 JSON 数据
|
|
$data = json_decode($postData, true);
|
|
|
|
// 检查是否成功解析
|
|
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
|
echo json_encode(['error' => 'JSON 解析错误: ' . json_last_error_msg()]);
|
|
exit;
|
|
}
|
|
|
|
// 处理数据
|
|
$standby = isset($data['standby']) ? $data['standby'] : 0;
|
|
$reboot = isset($data['reboot']) ? $data['reboot'] : 0;
|
|
$humidifier = isset($data['humidifier']) ? $data['humidifier'] : 0;
|
|
$screen = isset($data['screen']) ? $data['screen'] : 1;
|
|
|
|
|
|
// 处理逻辑示例
|
|
$response = [
|
|
'message' => '数据接收成功',
|
|
'standby' => $standby,
|
|
'reboot' => $reboot,
|
|
'humidifier' => $humidifier,
|
|
'screen' => $screen,
|
|
];
|
|
|
|
// 返回 JSON 响应
|
|
|
|
$queryString = "UPDATE `status` SET `standby`='$standby', `reboot`='$reboot', `humidifier`='$humidifier', `screen`='$screen'";
|
|
mysqli_query($conn, $queryString);
|
|
|
|
$queryString = "select * FROM status";
|
|
$result = mysqli_query($conn, $queryString);
|
|
if(mysqli_num_rows($result) > 0){
|
|
$data = []; // 初始化为空数组
|
|
while ($row = mysqli_fetch_object($result)) { // 获取查询结果作为对象
|
|
$data[] = $row; // 将对象追加到数组中
|
|
}
|
|
echo substr(json_encode($data),1,58) ; // 输出 JSON 格式的响应
|
|
}
|
|
}else{
|
|
$queryString = "select * FROM status";
|
|
$result = mysqli_query($conn, $queryString);
|
|
if(mysqli_num_rows($result) > 0){
|
|
$data = []; // 初始化为空数组
|
|
while ($row = mysqli_fetch_object($result)) { // 获取查询结果作为对象
|
|
$data[] = $row; // 将对象追加到数组中
|
|
}
|
|
echo substr(json_encode($data),1,58) ; // 输出 JSON 格式的响应
|
|
}
|
|
} |