为了 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");
}
}