可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Ok, so in my web app's API I have an incoming HTTP post request.
I would like to pass that POST request on to a different server, without losing the data in the POST header. Is this possible? which type of redirect would I use? php examples?
Edit: The HTTP request is coming from a mobile app, not a web browser.
Thanks!
回答1:
You could use cURL or sockets to re-post the data, but you can't really redirect it.
POST'ing to a URL with cURL:
$ch = curl_init('http://www.somewhere.com/that/receives/postdata.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
回答2:
RewriteRule current-page.php http://www.newserver.com/newpage.php [NC,P]
The P on there (proxy) will preserve the POST data. You'll need to turn on the apache proxy module if it isn't already.
回答3:
I know this is an old question but this may help people who stumble upon this question. You should be able to send an HTTP 307 response code to make the user agent redirect to the new url and continue to use the same method and data. This answer has more details
回答4:
If the client (ie the mobile app) HTTP library supports this, then you can return HTTP 307 from server which states that "the request should be repeated with another URI ... with the same method". This is essentially a temporary redirect but tells the client to use the the same method, a POST.
The client making the request must be able to respond accordingly to the HTTP 307 response and follow the redirection with the same method - for many libraries this may be an additional flag or setting.
回答5:
You cannot tell a browser to make a post request through an HTTP header. The location
header will redirect, but only for GET or HEAD requests.
You can work around this limitation by displaying a page with a hidden form with the method attribute set to POST and the action set to the URL you want the browser to post to, then automatically submit it on page load. Example:
<body onload="document.getElementById('form').submit();">
<form id="form" action="http://example.com/form_handler.php" method="POST">
<input type="hidden" name="param_1" value="data">
</form>
</body>
Alternately, you can make the POST request on your server and then display the results.
回答6:
I used the following code to redirect a post. In my case I am using only application/octet-stream content type so make sure you take that into consideration.
$request = file_get_contents ( "php://input" );
$arrContextOptions=array(
"http" => array(
"method" => "POST",
"header" =>
'Content-Type: application/octet-stream'. "\r\n".
'Content-Length: ' . strlen($request) . "\r\n",
"content" => $request,
),
"ssl"=>array(
"allow_self_signed"=>true,
"verify_peer"=>false,
),
);
$arrContextOptions = stream_context_create($arrContextOptions);
header ( "HTTP/1.1" );
header ( "Content-Type: application/octet-stream" );
$result = file_get_contents('http://thenewaddress.yes.it.works', false, $arrContextOptions);
file_put_contents("php://output", $result);
回答7:
I think the solution I'm about to go with is something like:
<?
$url = 'http://myserver.com/file.php';
foreach ($_POST as $key => $value) {
$text .= (strlen($text) > 0 ? '&' : '');
$text .= $key . '=' . $value;
}
header('Location: ' . $url . '?' . $text);
exit;
?>
Can anyone think of a reason why this is a bad idea?
回答8:
If you want to take data from a POST request and simply POST it to another server, then use cURL.
--or--
If you want to take data from a POST request and redirect the client to that other server while POSTing the data, then use this method...
Dynamically generate a form with all of the POST data. Something likes this...
echo "<form name=\"someform\" action=\"http://www.somewhereelse.com/someform.whatever\">";
foreach ($_POST as $key=>$value) {
echo "<input type=\"hidden=\" name=\"" . htmlspecialchars($key) . "\" value=\"" . htmlspecialchars($value) . "\" />";
}
echo "</form>";
Then, submit that form with some JavaScript when the page is done loading...
document.forms['someform'].submit();