title: HTTP Request with C; call PHP file and read the echo text output/response
body: <p>I simply want to read the response from a PHP file by calling it in C. </p>
  
  <p>At the moment I have the following PHP script and the C program:</p>
  
  <p>PHP file:</p>
  
  <pre><code>&lt;?php echo "successful"; ?&gt;
  </code></pre>
  
  <p>C code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  #include &lt;netinet/tcp.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  
  
  int socket_connect(char *host, in_port_t port){
      struct hostent *hp;
      struct sockaddr_in addr;
      int on = 1, sock;
  
      if((hp = gethostbyname(host)) == NULL){
          herror("gethostbyname");
          exit(1);
      }
      bcopy(hp-&gt;h_addr, &amp;addr.sin_addr, hp-&gt;h_length);
      addr.sin_port = htons(port);
      addr.sin_family = AF_INET;
      sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
      setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&amp;on, sizeof(int));
  
      if(sock == -1){
          perror("setsockopt");
          exit(1);
      }
  
      if(connect(sock, (struct sockaddr *)&amp;addr, sizeof(struct sockaddr_in)) == -1){
          perror("connect");
          exit(1);
  
      }
      return sock;
  }
  
  #define BUFFER_SIZE 1024
  
  int main(int argc, char *argv[]){
      int fd;
      char buffer[BUFFER_SIZE];
  
      fd = socket_connect("myserver.bplaced.net", 80);
      write(fd, "GET /MySQLadmin/myscript.php HTTP/1.0\r\n\r\n", strlen("GET /MySQLadmin/myscript.php HTTP/1.0\r\n\r\n")); // write(fd, char[]*, len);
      bzero(buffer, BUFFER_SIZE);
  
      while(read(fd, buffer, BUFFER_SIZE - 1) != 0){
          fprintf(stderr, "%s", buffer);
          bzero(buffer, BUFFER_SIZE);
      }
  
      shutdown(fd, SHUT_RDWR);
      close(fd);
  
      return 0;
  }
  </code></pre>
  
  <p>So far I can contact the server but when I read the BUFFER I receive the following but not the echo text.</p>
  
  <p>Output in the console:</p>
  
  <pre><code>HTTP/1.1 302 Found
  Date: Fri, 05 Aug 2016 05:21:14 GMT
  Server: Apache/2.4
  Location: http://www.bplaced.net/404
  Content-Length: 271
  Connection: close
  Content-Type: text/html; charset=iso-8859-1
  
  &lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;title&gt;302 Found&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;Found&lt;/h1&gt;
  &lt;p&gt;The document has moved &lt;a href="http://www.bplaced.net/404"&gt;here&lt;/a&gt;.&lt;/p&gt;
  &lt;hr&gt;
  &lt;address&gt;Apache/2.4 Server at default Port 80&lt;/address&gt;
  &lt;/body&gt;&lt;/html&gt;
  
  Process returned 0 (0x0)   execution time : 0.126 s
  Press any key to continue.
  </code></pre>
  
  <p>Maybe I have a typo somewhere but can't find it.</p>
  
  <p>Please help</p>