How to get HTTP GET or POST from php?

Hello!

I need help to make a algorithm to get http request from php, for example... when i receive a response from php, i receive that:

GET /on HTTP/1.1
Host: 187.52.23.205:8083
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2

how i read only the "on" in "GET /on HTTP/1.1"??
sorry for my english errors.. i am brazilian.

thanks a lot!

Do you want to get /on with php? Or an arduino?

viniscarton:
how i read only the "on" in "GET /on HTTP/1.1"??

  1. read in the HTTP header one line after the other into a variable ("array of char")
  2. after receiving a full line, check if the start of the line is equal to "GET /on "

mistergreen:
Do you want to get /on with php? Or an arduino?

I need to read in arduino..
Thanks!

jurs:

  1. read in the HTTP header one line after the other into a variable ("array of char")
  2. after receiving a full line, check if the start of the line is equal to "GET /on "

Can you give me a simple example in arduino...? I am begginer in arduino..
Thanks for your answer!

Can you give me a simple example in arduino...?

Have you looked at the example Arduino-as-client? It makes a GET request, and processes the reply.

PaulS:
Have you looked at the example Arduino-as-client? It makes a GET request, and processes the reply.

Thanks for you answer,
My doubt is read a variable value as a time for an alarm or anything or password... i dont know how to read the varibales values in "GET / ", i just know get values predefined...

viniscarton:
Can you give me a simple example in arduino…? I am begginer in arduino…

Here is some HTTP server example code that retrieves the requested “file” and “parameter” from a HTTP request and can react on them. Please set MAC and IP address to values that are unique in your local network.

Currently supported for handling are URL adresses like:
http://192.168.2.250/on
http://192.168.2.250/off

But also all other requests are answered with a “plain text” response to avoid your application from blocking for 30 or 45 seconds (typical HTTP timeout duration) when sending invalid requests using a webbrowser.

After a reqest is parsed, the requested “path and file name” is available in the global char arrays “httpFile” and the extra parameters sent is available in the char array “httpParam”. Watch out for serial debug! So you could change the source code easily to handle possible requests like:
http://192.168.2.250/motor?on
http://192.168.2.250/led?off

Here is the Arduino webserver code:

// Ethernet GET request example by 'jurs', created for Arduino forum
// http://forum.arduino.cc/index.php?topic=325984.msg2251372#msg2251372

#include <Ethernet.h>
#include <SPI.h>

// the next two lines must contain unique MAC and IP address in your LAN network
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 250 };

EthernetServer server(80);

// the next two variables will be used by 'readClientRequest()' and 'executeClientRequest()'
char httpFile[13];
char httpParam[13];
    
void sendClientResponse(EthernetClient &client)
{
  // send a standard http response header
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/plain"));
  client.println(F("Connection: close"));
  client.println(F("Cache-Control: no-cache, no-store"));
  client.println(F("Pragma: no-cache"));
  client.println();
  client.println(httpFile);
  client.println(httpParam);
}

void readClientRequest(EthernetClient &client)
{ // read the complete client request (HTTP header lines)
  // header lines are temporarily stored in 'linebuf'
  // GET parameter is stored in global variable 'httpGet'
  //
  char linebuf[80];
  int charcount=0;
  char* strPtr;
  char* paramPtr;
  memset(linebuf,0,sizeof(linebuf));
  memset(httpFile,0,sizeof(httpFile));
  memset(httpParam,0,sizeof(httpParam));

  // an http request ends with a blank line
  boolean currentLineIsBlank = true;
  while (client.connected()) 
  {
    if (client.available()) 
    {
      char c = client.read();
      
      if (charcount<sizeof(linebuf)-1)
      {
        linebuf[charcount]=c;
        charcount++;
      }
//      Serial.write(c); // for serial debug if you like
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
      if (c == '\n' && currentLineIsBlank) {
        break; // end of header found, break while-loop
      }
      else if (c == '\n') 
      {
        // we have a complete line, look what's in it
        if (strncasecmp(linebuf,"GET ",4)==0) // this is a GET request
        { // extract the requested file and parameter
          strPtr= strchr(&linebuf[4],' ');
          strPtr[0]='\0';
          paramPtr= strchr(&linebuf[4],'?');
          if (paramPtr!=NULL)
          {
            paramPtr[0]='\0';
            paramPtr++;
            if (strlen(paramPtr)<sizeof(httpParam)) strcpy(httpParam, paramPtr);
          }
          strncpy(httpFile, &linebuf[4], sizeof(httpFile)-1);
          Serial.print(httpFile);Serial.print(" ? ");Serial.println(httpParam);
        }
        // now we are starting a new line
        currentLineIsBlank = true;
        memset(linebuf,0,sizeof(linebuf));
        charcount=0;
      } 
      else if (c != '\r') 
      {
        // you've gotten a character on the current line
        currentLineIsBlank = false;
      }
    } // if (client.available()) 
  } // while (client.connected()) 
} // void readClientRequest(EthernetClient &client)


void handleClientRequest()
{ // function handles requests
  
  if (strstr(httpFile,"/on")==httpFile)
    Serial.println("Requested action is 'on'");
  else if (strstr(httpFile,"/off")==httpFile)
    Serial.println("Requested action is 'off'");
}


void setup(){
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
  Serial.print("Server is at: ");Serial.println(Ethernet.localIP());
}


void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    readClientRequest(client);
    handleClientRequest();
    sendClientResponse(client);
    client.flush();
    delay(1);
    client.stop();
  }
}

jurs:
Here is some HTTP server example code that retrieves the requested “file” and “parameter” from a HTTP request and can react on them. Please set MAC and IP address to values that are unique in your local network.

Currently supported for handling are URL adresses like:
http://192.168.2.250/on
http://192.168.2.250/off

But also all other requests are answered with a “plain text” response to avoid your application from blocking for 30 or 45 seconds (typical HTTP timeout duration) when sending invalid requests using a webbrowser.

After a reqest is parsed, the requested “path and file name” is available in the global char arrays “httpFile” and the extra parameters sent is available in the char array “httpParam”. Watch out for serial debug! So you could change the source code easily to handle possible requests like:
http://192.168.2.250/motor?on
http://192.168.2.250/led?off

Here is the Arduino webserver code:

// Ethernet GET request example by 'jurs', created for Arduino forum

// http://forum.arduino.cc/index.php?topic=325984.msg2251372#msg2251372

#include <Ethernet.h>
#include <SPI.h>

// the next two lines must contain unique MAC and IP address in your LAN network
byte mac = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168, 2, 250 };

EthernetServer server(80);

// the next two variables will be used by ‘readClientRequest()’ and ‘executeClientRequest()’
char httpFile[13];
char httpParam[13];
   
void sendClientResponse(EthernetClient &client)
{
  // send a standard http response header
  client.println(F(“HTTP/1.1 200 OK”));
  client.println(F(“Content-Type: text/plain”));
  client.println(F(“Connection: close”));
  client.println(F(“Cache-Control: no-cache, no-store”));
  client.println(F(“Pragma: no-cache”));
  client.println();
  client.println(httpFile);
  client.println(httpParam);
}

void readClientRequest(EthernetClient &client)
{ // read the complete client request (HTTP header lines)
  // header lines are temporarily stored in ‘linebuf’
  // GET parameter is stored in global variable ‘httpGet’
  //
  char linebuf[80];
  int charcount=0;
  char* strPtr;
  char* paramPtr;
  memset(linebuf,0,sizeof(linebuf));
  memset(httpFile,0,sizeof(httpFile));
  memset(httpParam,0,sizeof(httpParam));

// an http request ends with a blank line
  boolean currentLineIsBlank = true;
  while (client.connected())
  {
    if (client.available())
    {
      char c = client.read();
     
      if (charcount<sizeof(linebuf)-1)
      {
        linebuf[charcount]=c;
        charcount++;
      }
//      Serial.write(c); // for serial debug if you like
        // if you’ve gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
      if (c == ‘\n’ && currentLineIsBlank) {
        break; // end of header found, break while-loop
      }
      else if (c == ‘\n’)
      {
        // we have a complete line, look what’s in it
        if (strncasecmp(linebuf,"GET “,4)==0) // this is a GET request
        { // extract the requested file and parameter
          strPtr= strchr(&linebuf[4],’ ‘);
          strPtr[0]=’\0’;
          paramPtr= strchr(&linebuf[4],’?’);
          if (paramPtr!=NULL)
          {
            paramPtr[0]=’\0’;
            paramPtr++;
            if (strlen(paramPtr)<sizeof(httpParam)) strcpy(httpParam, paramPtr);
          }
          strncpy(httpFile, &linebuf[4], sizeof(httpFile)-1);
          Serial.print(httpFile);Serial.print(” ? ");Serial.println(httpParam);
        }
        // now we are starting a new line
        currentLineIsBlank = true;
        memset(linebuf,0,sizeof(linebuf));
        charcount=0;
      }
      else if (c != ‘\r’)
      {
        // you’ve gotten a character on the current line
        currentLineIsBlank = false;
      }
    } // if (client.available())
  } // while (client.connected())
} // void readClientRequest(EthernetClient &client)

void handleClientRequest()
{ // function handles requests
 
  if (strstr(httpFile,"/on")==httpFile)
    Serial.println(“Requested action is ‘on’”);
  else if (strstr(httpFile,"/off")==httpFile)
    Serial.println(“Requested action is ‘off’”);
}

void setup(){
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
  Serial.print("Server is at: ");Serial.println(Ethernet.localIP());
}

void loop()
{
  EthernetClient client = server.available();
  if (client)
  {
    readClientRequest(client);
    handleClientRequest();
    sendClientResponse(client);
    client.flush();
    delay(1);
    client.stop();
  }
}

Thanks for your help!

Simple server code that uses String functions to determine if a particular command character string is contained in what is sent from the client.

//zoomkat 10-6-13
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href='/?on1' target='inlineframe'>ON</a>"); 
          client.println("<a href='/?off' target='inlineframe'>OFF</a>"); 

          client.println("<IFRAME name=inlineframe style='display:none'>");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

zoomkat:
Simple server code that uses String functions to determine if a particular command character string is contained in what is sent from the client.

//zoomkat 10-6-13

//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ’ instead of " in html ilnes
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip = { 192, 168, 1, 102 }; // arduino IP in lan
byte gateway = { 192, 168, 1, 1 }; // internet access via router
byte subnet = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

//enable serial data print
  Serial.begin(9600);
  Serial.println(“servertest1”); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

//read char by char HTTP request
        if (readString.length() < 100) {

//store characters to string
          readString += c;
          //Serial.print(c);
        }

//if HTTP request has ended
        if (c == ‘\n’) {

///////////////
          Serial.println(readString); //print to serial monitor for debuging

//now output HTML data header
            if(readString.indexOf(’?’) >=0) { //don’t send new page
              client.println(“HTTP/1.1 204 Zoomkat\r\n\r\n”);
            }
            else {
          client.println(“HTTP/1.1 200 OK”); //send new page
          client.println(“Content-Type: text/html”);
          client.println();

client.println("");
          client.println("");
          client.println(“Arduino GET test page”);
          client.println("");
          client.println("");

client.println(“

Zoomkat’s simple Arduino button

”);
         
          client.println(“ON”);
          client.println(“OFF”);

client.println("");

client.println("");
          client.println("");
            }

delay(1);
          //stopping client
          client.stop();

///////////////////// control arduino pin
          if(readString.indexOf(“on1”) >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println(“Led On”);
          }
          if(readString.indexOf(“off”) >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println(“Led Off”);
          }
          //clearing string for next read
          readString="";

}
      }
    }
  }
}

Thanks for you answer!