New to web programming

Hi.

Im going to be sending information to a server and ive found this code from a similar project that does the same thing. I was wondering if anyone could help me understand the parts that have '//' next to them.

The original is from:

http://stackoverflow.com/questions/34131879/write-data-from-arduino-to-server-using-php

void loop(void){

  if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
    connected = true;
    Serial.print("Status is ");
    Serial.println("42");
    Serial.println();
    Serial.println("Sending to Server: ");

    client.print("GET /php_get_write.php?state=");          //
    Serial.print("GET /php_get_write.php?state=");
    client.print("42");                                                    //
    Serial.print("42");

    Serial.println();
    client.println();
    client.println("HTTP/1.1\r\n");                              //
    Serial.println();
    Serial.println("HTTP/1.1\r\n");
    client.println("Host: localhost\r\n");                      //
    Serial.println("Host: localhost\r\n");
    client.println();
    client.println("User-Agent: Arduino\r\n");              //
    Serial.println("User-Agent: Arduino\r\n");
    client.println("Accept: text/html\r\n");                   //
    Serial.println("Accept: text/html\r\n"); 
    client.println();
    Serial.println();
    delay(1000);
}
else
{
    Serial.println("Cannot connect to Server");
}
  }
  else
  {
    delay(1000);
    while (client.connected() && client.available())
{
    char c = client.read();                                           //
    Serial.print(c);


}
Serial.println();
client.stop();
connected = false;
}
 }

Thank you :)

The first statement is sending a GET request to the server. The second statement is appending a value to the GET request. The third statement is sending the rest of the GET request. The next statement assumes that the server is hosting multiple domains, and, therefore, needs to be told which domain the script belongs to (a usually incorrect assumption, but one that doesn't cause any harm).

The next set are sending data that is most likely not needed. It is possible for the server to tailor the output if it knows what the requesting device is capable of, but that is not necessary information to send, and most scripts/servers ignore the values.

Then, the reply from the server is read.

(“GET /php_get_write.php?state=”);

Could you break that down for me please. Im guessing this code wont work for me?

Or if it could be condensed, How? so i know what to research

Could you break that down for me please.

The GET part is the command type. The /php_get_write.php part is the name and path of the script to execute on the server. The ? indicates the start of name=value pairs. The name in the example is state. The value is added later (42).

Im guessing this code wont work for me?

If you connect to the correct server, where you have a script called php_get_write.php, in the root directory of the server's web directory, then it should work.

Answered my question perfectly and i understand it now. Thank you very much!

Why is it GET as opposed to POST is it is sending the value 42 to the server?

Why is it GET as opposed to POST

Because GET is far simpler.

Can you explain these too. Correct me if i'm wrong

"HTTP/1.1\r\n" ??? please explain

"Host: localhost\r\n" ??? please explain

"User-Agent: Arduino\r\n" // i guess this tells the server what is making the request

"Accept: text/html\r\n" // makes sure it only accepts a .txt file? if it were XML, would it be 'XML' instead of 'text'?

"HTTP/1.1\r\n"

This says that the GET request is to be processed as an HTTP request, in version 1.1 format.

"Host: localhost\r\n"

This says that the host (server) is the machine making the request, which is absolute nonsense, since the Arduino can't possibly run a php script.

User-Agent: Arduino\r\n" // i guess this tells the server what is making the request

Yes.

"Accept: text/html\r\n" // makes sure it only accepts a .txt file? if it were XML, would it be 'XML' instead of 'text'?

XML in HTML format? I don't think so.