morison20 Posted March 18, 2009 Share Posted March 18, 2009 Hi, I want to transfer data using HTTP POST method. I am trying the following HTTP header and PHP script but don't get the data in server. I am getting 403 error. What's wrong? HTTP POST header POST /insert.php HTTP/1.1 Host: www.safecircuit.com Content-Type: text/plain; charset=iso-8859-1 Content-Length: 10 data=1234567890 insert.php <?php $str = $_POST['data']; echo $str; ?> Is it for php $_POST? I also need to know how to upload data by part. Will you please give an example of both HTTP header and php script of uploading data by part? FYI I couldn't find our any good resource or tutorial on this. I really appreciate your help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/ Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 First of all you are missing a ; after $str. Secondly what kind of data are you trying to pass to this page? Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787279 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 First of all you are missing a ; after $str. Secondly what kind of data are you trying to pass to this page? Ahh...I am sorry. That missing ';' is a typo. I am trying to pass text of size 5KB-16KB. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787280 Share on other sites More sharing options...
WolfRage Posted March 18, 2009 Share Posted March 18, 2009 This might be exactly what you are looking for, or it may not be. But I hope it gives you some ideas for solving your problem. I think I left out a semi-colon in my code so watch out for that. http://www.phpfreaks.com/forums/index.php/topic,241471.msg1126387.html#msg1126387 Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787282 Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 Where are you passing text from? Is it on a form on another page? Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787284 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 WolfRage >> I couldn't make the script work. I have modified your script like the following- $data = array ('data' => 'bar'); $data = http_build_query($data); $context_options = array ( 'http' => array ( 'method' => 'POST', 'header'=> "Content-type: text/plain; charset=iso-8859-1\r\n" . "Content-Length: " . strlen($data) . "\r\n", 'content' => $data ) ); echo $data; $context = stream_context_create($context_options) $fp = file_get_contents('http://url', FALSE, $context); ?> Now I am getting PARSE ERROR- HTTP/1.1 200 OK Date: Wed, 18 Mar 2009 01:05:37 GMT Server: Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_co lor PHP/5.2.5 X-Powered-By: PHP/5.2.5 Transfer-Encoding: chunked Content-Type: text/html 83 <br /> <b>Parse error</b>: syntax error, unexpected T_VARIABLE in <b>C:\xampp\ht docs\otter\insert.php</b> on line <b>47</b><br /> 0 In fact purpose of your script and my requirement doen't seems same. I am trying to - 1. send the data in plain text from an embedded device. It will request to the server using the HEADER. 2. PHP script will fetch the data and save in database. The data size is 5KB-16KB Please tell me what I am missing and suggest me how to solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787294 Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 That error means that your insert.php page has an error in it. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787299 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 Where are you passing text from? Is it on a form on another page? I am passing the text from an embedded internet device. This is not a form. Rather I am uploading data from the device in plain text and in server I need to fetch that data and save in database. Am I missing something in HTTP header? Or is the format wrong? Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787301 Share on other sites More sharing options...
WolfRage Posted March 18, 2009 Share Posted March 18, 2009 What is on line 47 of insert.php? And actually from the output my script did work, your script simply failed to put out the correct data. See my script will parse post data and send it to another page like you wanted it also grabs the contents of the script that parses the post data, making a complete exchange. The output you see is what was echoed by your script, thus the error is on your script. The fact that there is html in the output proves that my script recieved the file contents, and that you are not just seeing an error screen. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787304 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 What is on line 47 of insert.php? And actually from the output my script did work, your script simply failed to put out the correct data. See my script will parse post data and send it to another page like you wanted it also grabs the contents of the script that parses the post data, making a complete exchange. The output you see is what was echoed by your script, thus the error is on your script. The fact that there is html in the output proves that my script recieved the file contents, and that you are not just seeing an error screen. It seems I didn't understand your code properly. I am newbie in php. So, please don't mind my asking. I have modified your code in following way- $data = array ('data' => 'bar'); $data = http_build_query($data); $context_options = array ( 'http' => array ( 'method' => 'POST', 'header'=> "Content-type: text/plain; charset=iso-8859-1\r\n" . "Content-Length: " . strlen($data) . "\r\n", 'content' => $data ) ); echo $data; $context = stream_context_create($context_options) $fp = file_get_contents('http://url', FALSE, $context); ?> Now my POST request is - POST /otter/insert.php HTTP/1.1 Host: localhost Content-Type: text/plain; charset=iso-8859-1 Content-Length: 10 data=123452w435 What's wrong I am doing? Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787310 Share on other sites More sharing options...
WolfRage Posted March 18, 2009 Share Posted March 18, 2009 I fail to see where you have inserted your data into the data array. It should look something like this: <?php $data = array ('data' => '123452w435'); $data = http_build_query($data); $context_options = array ( 'http' => array ( 'method' => 'POST', 'header'=> "Content-type: text/plain; charset=iso-8859-1\r\n" . "Content-Length: " . strlen($data) . "\r\n", 'content' => $data ) ); //don't echo here you will break the process $context = stream_context_create($context_options) $fp = file_get_contents('http://url', FALSE, $context); echo $data; echo '<br> Now lets see what we got from the other page:<br>'; echo $fp; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787313 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 Hi WolfRage, The script is now working fine. But now how will I will fetch the data in server from my POST request from another client? Say I am sending POST request like the following to the script from my client device- POST /otter/insert.php HTTP/1.1 Host: localhost Content-Type: text/plain; charset=iso-8859-1 data=saoudftyaoweifhasjdfsdz Now, how will I get the value of data from this POST? I should remind you that I am sending POST request from a client device using python language. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787320 Share on other sites More sharing options...
WolfRage Posted March 18, 2009 Share Posted March 18, 2009 You can post it to this script first and then have this script create the data array, then it will proceed to post the data to the server and grab the results. If posting is too complex you can also send the data to this script via get. I don't know if this is neccessarily the best solution for your setup, but is should work. If I understood your setup better I might be able to offer a better solution but that should work. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787323 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 You can post it to this script first and then have this script create the data array, then it will proceed to post the data to the server and grab the results. If posting is too complex you can also send the data to this script via get. I don't know if this is neccessarily the best solution for your setup, but is should work. If I understood your setup better I might be able to offer a better solution but that should work. I guess I couldn't make my setup clear. Let me try to clarify the setup once again- 1. I have a device which will connect to server and send data using HTTP POST. It is programmed using python. 2. When php server will receive data using POST request, it will process data and save it to database. Main 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. Now, please suggest me a simple POST method and PHP script that works. Say my POST method is the following- POST /insert.php HTTP/1.1 Host: www.mysite.com Content-Type: text/plain; charset=iso-8859-1 Content-Length: 10 data=1234567890 And server side script is the following- <?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? Please suggest me. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787333 Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 The PHP script should be fine, but your header is slightly wrong. Content-Length: 10 data=1234567890 But the length is 15. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787350 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 The PHP script should be fine, but your header is slightly wrong. Content-Length: 10 data=1234567890 But the length is 15. Hi corbin, I fixed that length, thanks. But the problem remains same. The server receives no data. The output is following- HTTP/1.1 200 OK Date: Wed, 18 Mar 2009 03:30:00 GMT Server: Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_co lor PHP/5.2.5 X-Powered-By: PHP/5.2.5 Content-Length: 0 Content-Type: text/html Connection to host lost. Can you please send me a simple POST header sending data to server and php script showing the data as output after receiving it? Or, at least run my code in telnet and tell me where to change? I am banging my head for last 2 days only after this thing. It will be a great help. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787361 Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 Ohhhhhhhhhhhhhhh!!! Wow.... Took me forever to realize something. Content-Type has to be: Content-Type: application/x-www-form-urlencoded Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787370 Share on other sites More sharing options...
morison20 Posted March 18, 2009 Author Share Posted March 18, 2009 Ohhhhhhhhhhhhhhh!!! Wow.... Took me forever to realize something. Content-Type has to be: Content-Type: application/x-www-form-urlencoded DAMN!! Man !! You are great!!! AHHHH... What the hell I was thinking !! I thought since I was not encoding the data and since data is plain text so I used plain text... Add this made me mad. Thanks a lot corbin. It is a really great help. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787376 Share on other sites More sharing options...
WolfRage Posted March 18, 2009 Share Posted March 18, 2009 Just as a final comment my original code did state that the header should be "Content-type: application/x-www-form-urlencoded\r\n" . Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-787727 Share on other sites More sharing options...
morison20 Posted March 19, 2009 Author Share Posted March 19, 2009 Just as a final comment my original code did state that the header should be "Content-type: application/x-www-form-urlencoded\r\n" . Surely it did. Thanks a lot to you also. In fact I acted like a goat. I thought I will need to encode the text into some form to use that format. And so I changed the content-type into text/plain Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/149907-solved-data-trasfer-using-http-post-and-php-script/#findComment-788655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.