PHP / CURL - 向另一个页面发出POST请求并跟踪URL

时间:2017-11-18 22:11:07

标签: php redirect curl header

我将尝试尽力解释这一点,我希望我有道理,因为英语不是我的第一语言。

假设我在第1页上有一个表格,其中包含以下3个输入:

$_POST["name"];
$_POST["amount"];
$_POST["email"];

在第二页process.php上我还有另一个输入:

$_POST["message"];

最后在外部网站上有一个我希望POST的表单,而不必在他们的系统中输入任何内容:

<form action="display.php" method="post">
 <input type="text" name="name"><br>
 <input type="text" name="amount"><br>
 <input type="text" name="email"><br>
 <input type="text" name="message"><br>
<input type="submit">
</form>

然后我希望能够从我的process.php页面自动重定向到www.example.com/display.php 并自动提交表单并能够查看我的所有信息我刚进入

display.php的示例:

Display.php

我无法访问外部网站或display.php上的代码,因此这意味着我无法编辑代码或使用任何会话。

我希望你能理解我,并且能够帮助我,如果你能帮助我,我将非常感激!

编辑:

我测试了一些我在答案中给出的代码并且看起来很有希望,但它没有将POST数据传递到外部页面(这不是用于测试目的的外部)

以下是用于测试的代码:

process.php

<?php
$_POST["name"] = "name";
$_POST["amount"] = "amount";
$_POST["email"] = "email";
$_POST["message"] = "message";

$post = array();
$post[] = "name=" . $_POST["name"];
$post[] = "amount=" . $_POST["amount"];
$post[] = "email=" . $_POST["email"];
$post[] = "message=" . $_POST["message"];

$ch = curl_init('http://localhost/display');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
header('Content-Type: text/html');
echo curl_exec($ch);
?>

Display.php的

<?php
echo $_POST["name"];
echo $_POST["amount"];
echo $_POST["email"];
echo $_POST["message"];
?>

但由于某种原因,这不起作用?

3 个答案:

答案 0 :(得分:0)

您可以process.php使用JavaScript创建并提交表单,如下所示:

<form id="form" action="display.php" method="post">
<input type="hidden" name="name" value="<?= $_POST["name"] ?>">
<input type="hidden" name="amount" value="<?= $_POST["amount"] ?>">
<input type="hidden" name="email" value="<?= $_POST["email"] ?>">
<input type="hidden" name="message" value="<?= $_POST["message"] ?>">
</form>

<script>document.getElementById("form").submit();</script>

希望我帮忙! -CM

答案 1 :(得分:0)

这个怎么样?它假设名称,金额和电子邮件将发布到process.php

添加了故障排除代码。

<强> process.php

 <?php

 //[put database queries here]

 // $message = ??????????????????


$message = 'Message';

$post = array(
'name'=>$_POST['name'],
'amount'=>$_POST['amount'],
'email'=>$_POST['email'],
'message'=>$message);

$ch = curl_init('http://www.example.com/display.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");

header('Content-Type: text/html');
$data = curl_exec($ch);
echo $data;
?>

这已经过测试并且也可以使用

重定向到实际网站。

<?php
$name = $_POST["name"];
$amount = $_POST["amount"];
$email = $_POST["email"];
$message = $_POST["message"];

echo <<< EOT
<html><head><style></style></head><body>
<form id="form" action="http://www.example.com/display.php" method="post">
<input type="hidden" name="name" value="$name"/>
<input type="hidden" name="amount" value="$amount"/>
<input type="hidden" name="email" value="$email"/>
<input type="hidden" name="message" value="$message"/>
</form>

<script>document.getElementById("form").submit();</script>
</body></html>
EOT;

?>

答案 2 :(得分:0)

服务器上的脚本无法以与浏览器相同的方式发布到页面,因此严格来说,您要求的内容无法完成。你可以做类似的事情,但仍然可以解决你的问题......

1)process.php可以使用Curl调用display.php并直接在process.php上显示结果

2)带有表单的原始页面可以执行一些Ajax(或类似的)来确定$ message应该是什么,然后通过JavaScript将其添加为隐藏输入,然后才提交表单。没有中间process.php形式 - 我们直接进入display.php

我认为#2最适合你描述的情况