Send data via POST method to website

Hi.

I try to send data to website via POST method.

I am using NodeMCU 1.0 (ESP-12E Module).

PHP code

<?php
    $temperature = $_POST["temperature"];
    $write = "<p>Teplota: ".$temperature."</p>";
    file_put_contents("teplota.php", $write);
?>

Arduino IDE

#include <Wire.h>
#include <ESP8266WiFi.h>
#include "SparkFunHTU21D.h"

WiFiClient client;

int temperature = 30;

void setup() {
    Serial.begin(9600);

    WiFi.begin("admin", "admin");
    while(WiFi.status() != WL_CONNECTED) {
        Serial.println("Repeating.");
        delay(500);
    }
    Serial.println("Úspěšně připojeno.");
    Serial.print("IP adresa: ");
    Serial.println(WiFi.localIP());
}

void loop() {
    String data = "temperature=" + temperature;
    
    if(!client.connect("arduino-zkouska.borec.cz", 80)) {
        Serial.println("Připojení k serveru nebylo úspěšné.");
        return;
    }

    client.println("POST /ulozit.php HTTP/1.1");
    client.println("Host: arduino-zkouska.borec.cz");
    client.println("Accept: */*");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    delay(500);
    Serial.println("Data poslány.");

    if(client.connected()) {
        client.stop();
    }

    delay(5000);
}

But on teplota.php I just have

Teplota

without value.

What is my mistake?

Do you see the response from the web server? I think the problem is your Content-Length, you are sending just the length of the string, you need to send the lenght of the whole request.

geologic:
Do you see the response from the web server? I think the problem is your Content-Length, you are sending just the length of the string, you need to send the lenght of the whole request.

the string data is the body and content length should be length of the body of the request. his has it right

Martin, print the response to Serial

How please?

I did not find the code, that print me the response.

see the WebClient example

OK, I appended this code between

Serial.println("Data poslány.");

and

if(client.connected())/code]

code:

while (client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
    }

Is this all right?

I get

HTTP/1.1 302 Found
Server: nginx
Date: Tue, 02 Oct 2018 19:19:39 GMT
Content-Type: text/html
Content-Length: 0
Connection: close
Location: teplota.php

the server responded with 302, which means redirect to "Location". but the Location is same. the only reason I can think for this is that the server doesn't accept POST. try GET request

Do you think POST from another device? Because POST from another web page on the same domain works.

GET doesn't work too.

try

String data = String("temperature=") + temperature;

Omg, it works.

But why I have to convert string to string?

because the + operator is overloaded on String class as concatenation. for c-string it is standard + on pointer value