在 esp32 上解析来自 php 服务器的 JSON 数据

问题描述 投票:0回答:1

所以我在解析从 php 服务器作为有效负载接收的 JSON 数据时遇到问题。 当我按下订单按钮时,服务器会在 order_pending 表中插入 order_pending 行,并用 id 和其他一些参数填充它。 PHP代码是:

if ($_SERVER["REQUEST_METHOD"] == "GET") {
    $api_key = test_input($_GET["api_key"]);
    if($api_key == $api_key_value) { 
        // Create connection
        $conn = new mysqli($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
    
        
        $sql = "SELECT locker_id,quantity_ordered,quantity,payload FROM orders_pending WHERE order_id=(SELECT MIN(order_id) FROM orders_pending WHERE locker_id!=404)";
        $data=mysqli_query($conn,$sql);
        while($row = mysqli_fetch_assoc($data))
        $test = $row["locker_id"]; 
        if($test==NULL)
        {
            $row["locker_id"]="404";
        }
        ob_clean();
        header_remove();
        
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($row);
    
        $conn->close();
    }
    else {
        echo "Wrong API Key provided.";
    }

}
else {
    echo "No data posted with HTTP POST.";
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

esp32代码是:

String checkPendingOrder()
{
  int lockerid = 0;
  String payload;
  

   //Check WiFi connection status
  if(WiFi.status() == WL_CONNECTED)
  {
    WiFiClient *client = new WiFiClient;
    //client->setInsecure(); //don't use SSL certificate
    HTTPClient http;
 
    // Your Domain name with URL path or IP address with path
    String serverPath = serverName + "?api_key=tPmAT5Ab3j7F9";
    http.useHTTP10(true);
    http.setTimeout(10000);
    http.begin(serverPath.c_str());
 
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
 
    // Send HTTP GET request
    int httpResponseCode = http.GET();
    //payload => variable which contains info about the lockers in DB
    String payload = http.getString();

if (httpResponseCode > 0) 
    {
      Serial.print("HTTP Response code check: ");
      Serial.println(httpResponseCode);
    }
    else 
    {
      Serial.print("Error check code check: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }
  else 
  {
    Serial.println("WiFi Disconnected");
  }
  
    return payload;}

void loop() 
{
  
  //inputs for locker feedback
  /*
  int l1 = digitalRead(lock1);
  int l2 = digitalRead(lock2);
  */

  //the locker id from the DB is assigned to the int variable lockerid
  //JsonDocument data= checkPendingOrder();
  //int lockerid=data["locker_id"].as<int>();
  //serializeJsonPretty(data,Serial);
 // scale.power_up();
  String data =checkPendingOrder();
  JsonDocument doc;
  deserializeJson(doc,data);
  int lockerid=doc["locker_id"];}

我得到的输出总是:

HTTP Response code check: 500
0

我想知道我是否发送了正确的 json 数组或者我是否解析正确?

php json esp32 arduinojson
1个回答
-2
投票

最好先测试 php api,你可以使用 postman,这样你就会知道它提供的任何响应。

© www.soinside.com 2019 - 2024. All rights reserved.