乐鑫技术交流
直播中

小峰

13年用户 951经验值
私信 关注
[问答]

如何通过PHP代码将日期发布到数据库?

为了 ISP 的安全,我无法直接连接到数据库服务器,所以我需要通过 PHP 文件使用 API 发送数据,PHP 文件又将数据存储到数据库服务器中。
PHP 文件通过 html 形式或通过 python 代码工作正常,所以我很确定我遇到的问题是我的代码。
执行代码时收到的错误是“无法连接”。我只能假设这是一个 SSL 连接问题,因为 ISP 网络服务器需要安全连接。
我怎么做?
tiA
代码:全选const char* dbServerName = "https://www.mydomain.com/post_data.php";

void db_send() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
     
    HTTPClient http;

    // Your Domain name with URL path or IP address with path
    http.begin(dbServerName);
   
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    //concat data to be sent
    String httpRequestData = "api_key=" + apiKeyValue +
                             "&temp=" + String(t) +
                             "&humidity=" + String(h) +
                             "&board=" + board +
                             "";
   
   //let's see what is being sent
    Serial.print("httpRequestData: "); Serial.println(httpRequestData);

    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: "); Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: "); Serial.println(httpResponseCode); // if -1 connection failed!!
    }

    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
}

                  

更多回帖

发帖
×
20
完善资料,
赚取积分