【问题标题】:PHP Form POST to external URL with Redirect to another URLPHP 表单 POST 到外部 URL 并重定向到另一个 URL
【发布时间】:2010-05-03 01:29:23
【问题描述】:

所以,我想要完成的是有一个自发布的 PHP 表单,发布到外部页面(使用 CURL),然后重定向到另一个页面。目前,正在发生的事情是,一旦我在表单(在contact.php 中)单击“提交”,它就会向自己发布(因为它是一个自发布表单)。然后该脚本使用 CURL 准备 POST 并执行该帖子。外部页面进行处理,然后外部页面应该重定向回引用域中的另一个页面。然而,实际上发生的情况是,contact.php 页面似乎从外部页面重定向到的页面加载 HTML,然后在同一页面上加载 contact.php 的 HTML。

效果,看起来像是两个单独的页面呈现为一个页面。自然,我只想执行 POST 并让浏览器呈现它应该重定向到外部页面指定的页面。

这是我目前的代码:

<?php     
if(isset($_POST['submit']))
 {
  doCURLPost();
 }

 function doCURLPost()
 { 
  $emailid = "2, 4";

  $hotel = $_POST['hotel'];


  //you will need to setup an array of fields to post with
  //then create the post string

  $data = array ( "recipient" => $emailid,
      "subject" => "Hotel Contact Form",
      "redirect" => "http://www.localhost.com/thanx.htm",
      "Will be staying in hotel: " => $_POST['hotel'],
      "Name" => $_POST['Name'],
      "Phone" => $_POST['Phone'],
      "Comments" => $_POST['Comments']);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://www.externallink.com/external.aspx");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_POST, true);  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer: http://www.localhost.com/contact.php"));
  $output = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
 }
?>

【问题讨论】:

    标签: php html url post external


    【解决方案1】:

    看来你快到了。

    在显示从 cURL 返回的内容后,您可能只想 die() 或 exit(),这样您的 pre-POST 版本的 contact.php 就不会被发送到浏览器。

    因此,您可以暂停执行,或使用条件构造代码,以便 “显示表单”的事情发生了,或者“做一些卷曲的帖子并显示结果”的事情发生了,但不是两者兼而有之。

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多