PHP,如何重定向/转发HTTP请求与标题和正文?
我有一个PHP页面,main.php是在服务器1上。
I have a PHP page, main.php which is on server 1.
我有一个PHP页面main.php(相同的页面,不同的代码) 2。
I have a PHP page main.php (same page, different code) on server 2.
main.php是一个WebService。
main.php is a WebService.
我想转发完整的HTTP请求服务器1,到服务器2,
,使得当用户向main.php(服务器1)发送HTTP请求时,它将从服务器2上的main.php获得响应。
I would like to forward the full HTTP request made to server 1, to server 2, so that when a user sends an HTTP request to main.php (server 1), it would get the response from main.php on server 2.
我希望对服务器2的请求与对服务器1的请求完全一样。
I would like the request made to server 2 to be exactly like the original request to server 1.
我通过以下方式获取Http请求数据:
I take the Http Request data via:
$some_param = $_REQUEST['param']
$body =file_get_contents('php://input');
并假设我有
$server1_url = "11111";
$server2_url = "22222";
这样做的动机是,我有一个生产服务器和临时服务器,我想直接一些流量到新服务器以测试临时服务器上的新功能。
The motivation for this is, i have a production server and a staging server, i would like to direct some traffic to the new server to test the new functionality on the staging server.
如何重定向所有数据的请求或克隆完整的请求,发送它到新的服务器并返回新的响应?
How do i redirect the request with all the data or "cloning" the full request, sending it to the new server and returning the new response?
感谢您的帮助!
php curl,但我不明白它是如何工作的,我发现各种答案,但没有转发请求参数和身体。
p.s i tried using php curl, but i dont understand how it works, also i found all kinds of answers, but none forward the Requests params and the body.
再次感谢! >
Again thanks!
这是我发现的解决方案(可能会更好)
this is the solution i have found (there might be better)
public static function getResponse ($url,$headers,$body)
{
$params = '?' . http_build_query($headers);
$redirect_url = $url . $params;
$ch = curl_init($redirect_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if (!isset($response))
return null;
return $response;
}