Solved

How to Post data to a PHP webpage from an embedded C application using TCP/IP?

Posted on 2013-12-16
5
245 Views
Last Modified: 2015-05-28
I have an embedded microcontroller that has a working TCP/IP stack written in C, provided by Microchip.

I wish to Post a single phrase "TestPhrase" to a field called "Data" at URL http://www.demoURL.com/TestData.php

I need to be able to post this phrase using C only.

I currently know the basics of html and nothing about PHP.

How should I achieve this?

Many thanks, Jamist
0
Comment
Question by:Jamist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39721643
In PHP you can find the contents of the POST request in the variable $_POST.  See this for more:
http://www.php.net/manual/en/tutorial.forms.php
http://www.php.net/manual/en/reserved.variables.post.php

The request would probably be made visible with something like this:
<?php var_dump($_POST);

Open in new window

The data would be accessible with something like this:
<?php echo $_POST['Data'];

Open in new window

0
 
LVL 2

Expert Comment

by:ITTallahassee
ID: 39721678
Ray_Paseur is correct.  If you are confused on the C Programming side, look into libcurl.  Using libcurl you can send information via C including POST data.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39721988
Why I like libcurl a lot myself, compiling it for a microcontroller might be a bit overkill, especially for just a post request, which you can send using the following code:

/*
 *                                 http_post.cpp 
 *
 * by Uday Chitragar - 2004/Dec/01
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must
 * not claim that you wrote the original software. If you use this
 * software in a product, an acknowledgment in the product documentation
 * would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 * 
 * */

/* 
 * Notes:
 * This source demonstrates sending HTTP POST request to webserver from C++
 * This uses sockets hence can be compiled on Linux, UNIX, Win
 */

#define LINUX_OS
// #define WIN_OS
#define _DEBUG_PRINT(X)   /* X */

//For commn
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#ifdef LINUX_OS
 #include <netdb.h>
#endif

#ifdef WIN_OS
 #include <Winsock2.h>
#endif


#define SEND_RQ(MSG) \
  send(sock,MSG,strlen(MSG),0);


using namespace std;
//<exe> hostname api parameters
int request (char* hostname, char* api, char* parameters)
{

	#ifdef WIN_OS
	{
		WSADATA	WsaData;
		WSAStartup (0x0101, &WsaData);
	}
	#endif

    sockaddr_in       sin;
    int sock = socket (AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
		return -100;
	}
    sin.sin_family = AF_INET;
    sin.sin_port = htons( (unsigned short)80);

    struct hostent * host_addr = gethostbyname(hostname);
    if(host_addr==NULL) {
      _DEBUG_PRINT( cout<<"Unable to locate host"<<endl );
      return -103;
    }
    sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
    _DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);

    if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
     _DEBUG_PRINT( cout<<"connect failed"<<endl ) ;
     return -101;
    }

 string send_str;

 SEND_RQ("POST ");
 SEND_RQ(api);
 SEND_RQ(" HTTP/1.0\r\n");
 SEND_RQ("Accept: */*\r\n");
 SEND_RQ("User-Agent: Mozilla/4.0\r\n");

 char content_header[100];
 sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
 SEND_RQ(content_header);
 SEND_RQ("Accept-Language: en-us\r\n");
 SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
 SEND_RQ("Host: ");
 SEND_RQ("hostname");
 SEND_RQ("\r\n");
 SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
 
 SEND_RQ("\r\n");
 SEND_RQ("\r\n");
 SEND_RQ(parameters);
 SEND_RQ("\r\n");

 #ifdef WIN_OS
   WSACleanup( );
 #endif

 return 0;
}


int main(){
  request ("www.somesite.com", "/post_url.pl", "Data=TestPhrase");
  //  response! is thrown away.

  return 0;
}

Open in new window


The code was taken from http://www.codeguru.com/cpp/i-n/internet/http/article.php/c8813/HTTP-Post-Using-C.htm and modified to be plain C.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question