Go Down

Topic: HTTP Post Request(Arduino) to $_POST(PHP) (Read 364 times) previous topic - next topic

OninVR

How could I get HTTP Post Request from Arduino using $_POST of PHP? or is it impossible using that method? I can't get clear tutorial or article of this thing.


Any help would greatly be appreciated.

PieterP

#1
May 07, 2018, 12:13 pm Last Edit: May 07, 2018, 12:15 pm by PieterP
Read up on the HTTP protocol. You just have to open a TCP socket and print HTTP text to it.
You can open the development tools in your browser to see what text is sent when you make the request from a form within the web page.

Pieter

PaulRB

#2
May 07, 2018, 12:47 pm Last Edit: May 07, 2018, 12:53 pm by PaulRB
Your question is a little confusing! You can't run PHP on an Arduino, as far as I know. So where is the PHP running and which way is the POST request going? From the Arduino to some other server, or from a server to the Arduino? The $_POST variable is an array of all the parameters passed to the PHP script by a remote client. It is not a command to initiate a POST request to a remote server.

It would help clarify things if you described what your project is about in real-world terms. Then we could make some educated guesses.

OninVR

Your question is a little confusing! You can't run PHP on an Arduino, as far as I know. So where is the PHP running and which way is the POST request going? From the Arduino to some other server, or from a server to the Arduino? The $_POST variable is an array of all the parameters passed to the PHP script by a remote client. It is not a command to initiate a POST request to a remote server.

It would help clarify things if you described what your project is about in real-world terms. Then we could make some educated guesses.
I want my Arduino to sent HTTP POST Request to the server and I am new to this stuff so I don't know basics of it. I want the server to get the data and save it into my database. Any clues and references?
- Can PHP get that request on the server side?
- Do I have to open the browser all the time to make my PHP run?

PieterP

- Can PHP get that request on the server side?
Yes.
- Do I have to open the browser all the time to make my PHP run?
No, the browser is just a client, just like the Arduino is just a client. The server is a separate application, it doesn't require nor imply the use of a browser.

Pieter

PaulRB


OninVR

Code: [Select]
void httpRequest(String data)
{
  String request = "GET /sen_track/get.php?data=";
  request += data;
  request += " HTTP/1.1\r\nHost: 192.168.43.171 : 8080\r\nAccept: */*\r\nContent-Length: ";
  request += data.length();
  request +="\r\nContent-type: text/html\r\nConnection: close\r\n";
  Serial.println(request);
  espSerial();
  esp.print("AT+CIPSEND=");
  esp.println(request.length());
  delay(500);
  if(esp.find("OK"))
  {
    Serial.print("Sending");
    espSerial();
    esp.print(request);
    delay(200);
  }
  if( esp.find("SEND OK"))
  {
    Serial.println();
    Serial.println("Packet sent");
    if(esp.find("400"))
      Serial.println("Bad Request");
  }

  esp.println("AT+CIPCLOSE");
  espSerial();
}



I always get HTTP 1.1/ 400 Bad request.

Is there something wrong with my HTTP request? Help please.

Juraj

#7
May 15, 2018, 09:33 am Last Edit: May 15, 2018, 09:46 am by Juraj
do not add port to Host header, only the IP address remove the spaces around : in Host header and add an empty line after the last header.

remove the Content-lenght header if you have a GET request
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

PieterP

#8
May 15, 2018, 09:41 am Last Edit: May 15, 2018, 09:43 am by PieterP


Juraj

Sir PieterP,

What do you think are my mistakes?
It was my mistake, in my comment. read it
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up