可以使用 curl 函数来发送 HTTP 请求,将 'file_get_contents('php://input')' 作为 POST 请求的 body,通过设置 location header 来实现重定向到 b.php。

示例代码如下:

$url = 'http://example.com/b.php'; // b.php 的地址
$data = file_get_contents('php://input'); // 获取 POST 请求的 body
$headers = [
    'Content-Type: application/json', // 根据实际情况设置 Content-Type
    'Location: ' . $url, // 设置 location header
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response; // 输出 b.php 返回的结果

注意,这里假设要发送的数据是 JSON 格式,如果不是 JSON 格式需要根据实际情况修改 Content-Type。另外,需要根据实际情况修改 URL、data 和 headers。

PHP 使用 Location Header 重定向 POST 请求到另一个地址

原文地址: https://www.cveoy.top/t/topic/n4X2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录