【问题标题】:PHP/CURL - Making a POST request to another page and following the URLPHP/CURL - 向另一个页面发出 POST 请求并跟随 URL
【发布时间】:2018-05-02 11:11:42
【问题描述】:

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

假设我在第 1 页上有一个包含这 3 个输入的表单:

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

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

$_POST["message"];

最后,在一个外部网站上,我想发布一个表单,而无需在他们的系统中输入任何内容:

<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 数据传递到外部页面(这不是用于测试目的的外部页面)

下面是用来测试的代码:

进程.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);
?>

显示.php

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

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

【问题讨论】:

    标签: php redirect curl header


    【解决方案1】:

    这个怎么样?它假定姓名、金额和电子邮件将发布到 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;
    
    ?>
    

    【讨论】:

    • 第 1 页是一个类似于外部网站的表单,这是用户输入他们的姓名、电子邮件和金额的地方。
    • 然后表单转到 process.php 并将用户详细信息添加到数据库(Process.php 将设置一个用户未设置的预定义消息)
    • 然后我希望 process.php 重定向到 display.php(绕过外部网站上的表单)
    • 所以我需要以某种方式将这个 POST 数据传递给 display.php 而不使用 HTML 表单,这就是我想要做的,我希望它是可能的。
    • 非常感谢,希望你能稍微理解我一点,谢谢!
    【解决方案2】:

    服务器上的脚本不能像浏览器那样 POST 到页面,所以严格来说,你所要求的不能完成。您可以做类似的事情,但仍然可以解决您的问题...

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

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

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

    【讨论】:

      【解决方案3】:

      您可以让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

      【讨论】:

      • 我想知道如何使用 CURL 跟踪这些数据,这不适用于我正在从事的项目,因为它比这更复杂一些,非常感谢您的回答不过!
      • 另外,process.php 没有表单,只有第一页有!
      • process.php 都是 CURL 并且消息是预定义的,我需要重定向到 display.php,同时让所有 POST 数据都随用户提供
      • @rakupu 我将答案更新为更适合您的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      相关资源
      最近更新 更多