PHP - 通过 POST 重定向和发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3045097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Redirect and send data via POST
提问by TheFlash
I have an online gateway which requires an HTML form to be submitted with hidden fields. I need to do this via a PHP script without any HTML forms (I have the data for the hidden fields in a DB)
我有一个在线网关,需要提交一个带有隐藏字段的 HTML 表单。我需要通过没有任何 HTML 表单的 PHP 脚本执行此操作(我有数据库中隐藏字段的数据)
To do this sending data via GET:
为此,通过 GET 发送数据:
header('Location: http://www.provider.com/process.jsp?id=12345&name=John');
And to do this sending data via POST?
并通过POST发送数据?
采纳答案by symcbean
You can't do this using PHP.
您不能使用 PHP 执行此操作。
As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.
正如其他人所说,您可以使用 cURL - 但是 PHP 代码成为客户端而不是浏览器。
If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.
如果您必须使用 POST,那么唯一的方法就是使用 PHP 生成填充的表单并使用 window.onload 挂钩调用 javascript 来提交表单。
C.
C。
回答by rezashamdani
here is the workaround sample.
这是解决方法示例。
function redirect_post($url, array $data)
{
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function closethisasap() {
document.forms["redirectpost"].submit();
}
</script>
</head>
<body onload="closethisasap();">
<form name="redirectpost" method="post" action="<? echo $url; ?>">
<?php
if ( !is_null($data) ) {
foreach ($data as $k => $v) {
echo '<input type="hidden" name="' . $k . '" value="' . $v . '"> ';
}
}
?>
</form>
</body>
</html>
<?php
exit;
}
回答by newms87
Another solution if you would like to avoid a curl call and have the browser redirect like normal and mimic a POST call:
如果您想避免 curl 调用并让浏览器像平常一样重定向并模拟 POST 调用,则另一种解决方案:
save the post and do a temporary redirect:
保存帖子并进行临时重定向:
function post_redirect($url) {
$_SESSION['post_data'] = $_POST;
header('Location: ' . $url);
}
Then always check for the session variable post_data:
然后始终检查会话变量post_data:
if (isset($_SESSION['post_data'])) {
$_POST = $_SESSION['post_data'];
$_SERVER['REQUEST_METHOD'] = 'POST';
unset($_SESSION['post_data']);
}
There will be some missing components such as the apache_request_headers() will not show a POST Content header, etc..
会有一些缺失的组件,例如 apache_request_headers() 不会显示 POST Content 标头等。
回答by Matt
It would involve the cURL PHP extension.
它将涉及 cURL PHP 扩展。
$ch = curl_init('http://www.provider.com/process.jsp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=12345&name=John");
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); // RETURN THE CONTENTS OF THE CALL
$resp = curl_exec($ch);
回答by Eduardo Cuomo
/**
* Redirect with POST data.
*
* @param string $url URL.
* @param array $post_data POST data. Example: array('foo' => 'var', 'id' => 123)
* @param array $headers Optional. Extra headers to send.
*/
public function redirect_post($url, array $data, array $headers = null) {
$params = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data)
)
);
if (!is_null($headers)) {
$params['http']['header'] = '';
foreach ($headers as $k => $v) {
$params['http']['header'] .= "$k: $v\n";
}
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if ($fp) {
echo @stream_get_contents($fp);
die();
} else {
// Error
throw new Exception("Error loading '$url', $php_errormsg");
}
}
回答by MajidJafari
A better and neater solution would be to use $_SESSION:
更好更简洁的解决方案是使用 $_SESSION:
Using the session:
使用会话:
$_SESSION['POST'] = $_POST;
and for the redirect header request use:
对于重定向标头请求,请使用:
header('Location: http://www.provider.com/process.jsp?id=12345&name=John', true, 307;)
307is the http_response_codeyou can use for the redirection request with submitted POSTvalues.
307是http_response_code您可以用于带有提交POST值的重定向请求。
回答by svens
Use curl for this. Google for "curl php post" and you'll find this: http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html.
为此使用 curl。谷歌搜索“curl php post”,你会发现这个:http: //www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html。
Note that you could also use an array for the CURLOPT_POSTFIELDS option. From php.net docs:
请注意,您还可以将数组用于 CURLOPT_POSTFIELDS 选项。来自 php.net 文档:
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
要在 HTTP“POST”操作中发布的完整数据。要发布文件,请在文件名前加上 @ 并使用完整路径。这既可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...')传递,也可以作为以字段名称作为键和字段数据作为值的数组传递。如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。
回答by Glyn Mooney
Your going to need CURL for that task I'm afraid. Nice easy way to do it here: http://davidwalsh.name/execute-http-post-php-curl
恐怕你需要 CURL 来完成这项任务。在这里很简单的方法:http: //davidwalsh.name/execute-http-post-php-curl
Hope that helps
希望有帮助
回答by Tobias P.
回答by Rodolfo Jorge Nemer Nogueira
Alternatively, setting a session variable before the redirect and test it in the destination url, can solve this problem for me.
或者,在重定向之前设置一个会话变量并在目标 url 中测试它,可以为我解决这个问题。

