ESP8266 POST and GET to HTTP/PHP (NODEMCU, WEMOS)

Hi again :slight_smile:
im riky, here to ask question again
excuse me for my bad english…

i learn a little about html and php
i was made a button for led and connect it as command to my nodemcu and it works expected.

now im going to next level, but im stuck with POST…

i wil start with how to POST with ESP8266 ?

i want to POST some value to the internet, my own website, just a simple value or text
example
send it to http://mywebsite.com/read.php “HELLO WEBSITE” and save the text to text.log

i get some code from some website

here is the php code

<?php
$handle = fopen('text.log', 'w');
fwrite($handle, date('d/m/Y H:i:s') . " - " . $_POST['words']) . "\n";
fclose($handle);
?>

with ‘words’ as a variable $words to call my text

and this is the esp8266 side
i use nodemcu

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "------";
const char* password = "------";

void setup()
{
  Serial.begin(115200);
  Serial.println(" Hi! Riky");
  delay(1000);
  Serial.println("Connecting...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
  delay(1000);

  Serial.println(WiFi.localIP());
  delay(1000);
}

void loop()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;
    http.begin("http://mywebsite.com/read.php");
    http.POST("Content-Type: application/x-www-form-urlencoded");
    http.POST("POST /read.php HTTP/1.1");
    http.POST("Host: http://rwmarduino.000webhostapp.com");
    http.POST("Content-Length: 7");
    http.POST("");
    http.POST("words=HELLO WEBSITE");
    http.end();
  }
}

when i upload this sketch it work and access my site (http://mywebsite.com/read.php) and triggered /read.php side to write to file (text.file) but it won’t write my text (HELLO WEBSITE), the ‘read.php’ just write the time without my text…

here is the result from my website file (text.log)

07/05/2018 04:05:52 -

where is my mistake ?
is it the code on website ? or my sketch on arduino ?

can you help me to the next level ?? :slight_smile: :slight_smile:
any suggestions would be greatly appreciated

regards
RWM

Lets start with why you want to POST data. GET is far simpler conceptually. POST is necessary if you wish to hide the data being posted.

The body of the POST is "text=HELLO WEBSITE". How did you come to the conclusion that you would access that data using $_POST['words']?

thanks for the response sir

PaulS:
Lets start with why you want to POST data. GET is far simpler conceptually. POST is necessary if you wish to hide the data being posted.

The body of the POST is “text=HELLO WEBSITE”. How did you come to the conclusion that you would access that data using $_POST[‘words’]?

sorry forget to change its “words=HELLO WEBSITE”

i get the conclusion from my project before, and i read it from some website sensor send data to php

my project before is buttons control led

example for the php file

<?php
header('Location: index.html');
$path = 'data.txt';
 if (isset($_POST['field1'])) 
 {
    $fh = fopen($path,"w");
    $string = $_POST['field1'];
    fwrite($fh,$string);
    fclose($fh);
 }
?>

in the index.html i write form post method and a button. and the index.html give a value to php file in ‘field1’

and I conclude if i POST “words=HELLO WEBSITE” it will give a variable to $words and i thought it will access that data , and write it to file ‘text.log’
with this code

<?php
$handle = fopen('text.log', 'w');
fwrite($handle, date('d/m/Y H:i:s') . " - " . $_POST['words']) . "\n";
fclose($handle);
?>

what should i do sir ?

Up Up :slight_smile: :slight_smile:

come and help me sir >.< :slight_smile: :slight_smile:

what should i do sir ?

Explain why you insist on POSTing data that no one in their right mind would possibly want to intercept.

PHP has a way of examining all the $_POST array contents. Make use of that.

Post Arduino code and PHP sketches that are supposed to go together.