Page 1 of 1

Send data using HTTP POST to php script

Posted: Tue Mar 17, 2009 9:47 pm
by morison20
Hi,
My set up is following-
1. I have a device which will connect to server and send data using HTTP POST. It can be programmed using python.
2. When php server will receive that data through POST request from the client device, it will process data and save it to database.

My problem is
in communication where I send the data from device using the POST method above but in server side I don't receive any data. So, what may go wrong?

I believe problem may be in 2 places-
1. I am either making mistake in POST method.
2. Or, my server side script is wrong.

I am also testing it using telnet to my localhost.

My POST method is the following-

Code: Select all

POST /insert.php HTTP/1.1
Host: http://www.mysite.com
Content-Type: text/plain; charset=iso-8859-1
Content-Length: 10
 
data=1234567890
 
And server side script is the following-

Code: Select all

 
<?php
$str = $_POST['data'];
echo $str;
?>
 
Where is the problem? Shouldn't I use $_POST? Or, do I need a custom function to fetch data from the POST method above?

Now, please suggest me a simple POST method and PHP script that works.

Thanks.

Re: Send data using HTTP POST to php script

Posted: Wed Mar 25, 2009 2:09 am
by sujithtomy
Hello,

is your Host: http://www.mysite.com ??
then your php script should reside in http://www.mysite.com to receive post variables.

Re: Send data using HTTP POST to php script

Posted: Wed Mar 25, 2009 4:07 am
by php_east
morison20 wrote:Hi,
My set up is following-
I believe problem may be in 2 places-
1. I am either making mistake in POST method.
2. Or, my server side script is wrong.
HTTP HEADERS are notoriously strict about newlines and number of newlines and the final lines need have two newlines \n\n. Otherwise the server simple ignores your post. You may need to read RFCs, but if you make sure you produce valid HTTP Headers by taking real examples ( from browsers ), it would be sufficient.
morison20 wrote: Where is the problem? Shouldn't I use $_POST? Or, do I need a custom function to fetch data from the POST method above?
Now, please suggest me a simple POST method and PHP script that works.
Thanks.
no, i don't think you need a custom fetch, that would be reinventing the wheel. and i think your method is as simple as it gets, anything simpler would be gong down to bit levels, and would perhaps then be not simpler after all. so you are on the right track i think.