重定向HTTP POST

Ken*_*ker 3 php post redirect http

好的,所以在我的网络应用程序的API中,我有一个传入的HTTP发布请求.我想将POST请求传递给其他服务器,而不会丢失POST标头中的数据.这可能吗?我会使用哪种类型的重定向?php例子?

编辑:HTTP请求来自移动应用程序,而不是Web浏览器.

谢谢!

sim*_*aun 9

您可以使用cURL或套接字重新发布数据,但您无法真正重定向它.

使用cURL POST到URL:

$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);
Run Code Online (Sandbox Code Playgroud)


pro*_*php 6

RewriteRule current-page.php http://www.newserver.com/newpage.php [NC,P]
Run Code Online (Sandbox Code Playgroud)

那里的P(代理)将保留POST数据.如果尚未启用apache代理模块,则需要打开它.

  • +1这是正确的解决方案,PHP应该与它无关.HTTP服务器(例如Apache)和客户端(例如Firefox)旨在处理这些情况,这是它们符合的HTTP规范的一部分. (4认同)