You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32向本地PHP脚本发送HTTP POST请求返回-1错误码求助

ESP32 HTTP POST请求本地PHP脚本返回错误码-1的排查原因

问题背景

使用ESP32通过HTTP POST向本地XAMPP环境的PHP脚本发送数据时,始终返回错误码-1;但请求外部地址(如http://arduinojson.org/example.json)能得到非-1的响应(403禁止访问)。已尝试将URL中的localhost替换为电脑IP,问题仍未解决。

ESP32代码

#include <WiFi.h>
#include <HTTPClient.h>

#include <Wire.h>

//network credentials
const char* ssid     = "[My_WIFI_SSID]";
const char* password = "[My_WIFI_PASSWORD]";

//Domain name and URL path or IP address with path
const char* serverName = "http://[My_IP_ADDRESS]/insert_temp-post.php";
//using http://arduinojson.org/example.json instead -> httpResponseCode != -1

// Keep this API Key value to be compatible with the PHP code provided in the project page. 
// If you change the apiKeyValue value, the PHP file also needs to have the same key 
String apiKeyValue = "tPmAT5Ab3j7F9";

void setup() {
  Serial.begin(9600);
  
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  //Check WiFi connection status
  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
    
    // Domain name with URL path or IP address with path
    http.begin(client, serverName);
    
    //content-type header specification
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // Prepare HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&temperature=34.5";
    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);
    }
    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  //Send an HTTP POST request every 30 seconds
  delay(30000);  
}

可能的原因及排查方向

  • 本地网络连通性问题:
    • 确认ESP32与运行XAMPP的电脑处于同一WiFi网络,且电脑防火墙/杀毒软件未拦截80端口(HTTP默认端口)的入站请求。可临时关闭防火墙测试,或在防火墙上添加允许Apache服务通过的规则。
    • 核对电脑IP准确性:确保serverName中的[My_IP_ADDRESS]是电脑当前WiFi下的IPv4地址,而非IPv6或外网IP。可通过ipconfig(Windows)或ifconfig(Linux/macOS)查看。
  • XAMPP配置问题:
    • 确认Apache服务正常启动:打开XAMPP控制面板,检查Apache模块状态为"Running"。
    • 检查PHP脚本路径:确保insert_temp-post.php放在XAMPP的htdocs目录下(Windows默认路径为C:\xampp\htdocs),URL路径与实际文件路径一致,注意大小写(Linux/macOS区分大小写)。
    • 测试PHP脚本可用性:在电脑浏览器直接访问http://[My_IP_ADDRESS]/insert_temp-post.php,传递参数测试(如http://[My_IP_ADDRESS]/insert_temp-post.php?api_key=tPmAT5Ab3j7F9&temperature=34.5),排查脚本语法错误或数据库连接问题。
  • HTTPClient库连接问题:
    • 尝试增加连接超时时间:在http.begin后添加http.setTimeout(5000),设置5秒超时,避免因默认超时过短导致连接失败。
    • 排查代理干扰:若网络存在强制代理,可能影响连接,可在代码中禁用代理或调整网络环境。
  • PHP脚本请求处理问题:
    • 确认脚本正确处理POST请求:检查脚本中是否使用$_POST获取参数,而非$_GET,例如$api_key = $_POST['api_key'];。
    • 核对API密钥一致性:确保ESP32发送的apiKeyValue与脚本中的验证值完全一致,无空格或大小写差异。

内容的提问来源于stack exchange,提问作者Nicole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:08:16