2

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...

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

9 Comments

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 who is "me"? a browser?
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 that's impossible. Let user to deal with a payment gateway himself.
@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
|
1

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') );

Comments

1

The only way to redirect POST Header is an HTTP 1.1 Redirect with 307/308

307 Temporary Redirect

header('HTTP/1.1 307 Redirect');
header('Location: target.php', false, 307);

308 Permanent Redirect

header('HTTP/1.1 308 Redirect');
header('Location: target.php', false, 308);

!!! Only works in PHP if the Redirect Code is setted 2 times in this way !!! If you only do the 2nd line. PHP made a normal 302 redirect.

Client get a message from browser if to accept this redirect, because of security reasons.

1 Comment

@AndrewLBarber please dont delete agian. this is an right answer. in this way the server let know the client/browser that hes has send the post again zo another target

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.