tags:

views:

142

answers:

3

Hi.. I want send some data to a remote webpage from my site. Actually it can be achieved through form hidden variables. but for security reason, i want set as post variables in header and then send to that webpage. i use this code

$post_data = 'var1=123&var2=456';

$content_length = strlen($post_data);

header('POST http://localhost/testing/test.php HTTP/1.1');

header('Host: localhost');

header('Connection: close');

header('Content-type: application/x-www-form-urlencoded');

header('Content-length: ' . $content_length); 

header($post_data);

but my code doesn't work properly. help me...

+2  A: 

POST data forms the body of an HTTP request, it isn't a header, and the header method is used in making HTTP responses.

askapache has an example of making a POST request from PHP using the curl library.

David Dorward
thanks for your response
kumar_v
A: 

You're trying to put request headers into answer. Client just don't know what to do with it.

What you are trying to achieve is impossible.

What is the task you're trying to accomplish? There can be another way.

Col. Shrapnel
actually i want send some data as post data to that webpage. they will validate my data. if valid, they will allow me to access their site. or they will return error msg.
kumar_v
@kumar who is "me"? a browser?
Col. Shrapnel
my site contain payment section. i want pass customer details to payment gateway site. so i'm getting user details from db and set as post variable in header. then i request the webpage with my user details. if data given by user is valid, then user can do payment process. this is the process.
kumar_v
@kumar that's impossible. Let user to deal with a payment gateway himself.
Col. Shrapnel
but it's possible with form hidden value. but for security reason, i'm doing like this.
kumar_v
@kumar you're **not** doing like this because it's impossible. And you'd better start to understand that. And for the security reasons do not put your hands on the client's personal data, and let him to interact with a payment gateway himself. Or you'll be in trouble with law.
Col. Shrapnel
not misusing users data. Actually we get users data like name, address only. not card information. so users will give their card info at payment site only. but i want pass quantity, and user id to payment url as return url.
kumar_v
as u want to pass the quantity, this quantity field could also be altered, not done, u should not keep hand on whatever user has ordered whether its product or its other parameters....let user deal himself.. as Col.Shrapnel said...
OM The Eternity
@kumar you don't need to pass name or address but only **order ID**. That's enough. And it's secure. Stop being a fool and start reading payment gateway's documentation - they have covered every your move already
Col. Shrapnel
ok i understood. thanks for your kind replies
kumar_v
A: 

If you're trying to post a request to remote server, you'll need to use a tool like cUrl. Here's an exmple:

// Create a curl handle to a non-existing location
$ch = curl_init('http://localhost/testing/test.php');

// Set options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute
$page = curl_exec($ch); 

Alternatively, if you really want to put data in the headers, you can set custom headers using something like this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('field: value', 'field2: value') );
Chris Henry