I want send some data to a remo开发者_StackOverflow中文版te 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...
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.
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.
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') );
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.
精彩评论