Jump to content

Recommended Posts

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.

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

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.

 

 

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?

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.

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?

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;
?>

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.

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.

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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.