开发者

PHP Form POST to external URL with Redirect to another URL

So, what I am trying to accomplish is have a self-posting PHP form, POST to an external page (using CURL) which in turn redirects to another page. Currently, what is happening is that once I click "Submit" on the form (in contact.php) it will POST to itself (as it is a self-posting form). The script then prepares the POST using CURL and performs the post. The external page does its processing and then, the external page is supposed to redirect back to another page, in a referring domain. However, what happens instead, is that it seems like the contact.php page loads the HTML from the page the external page redirected to, and then, the contact.php's HTML loads after that, ON THE SAME PAGE.

The effect, is what looks like two separate pages rendered as one page. Naturally, I just want to perform the POST and have the browser render the page it is supposed to redirect to as specified by the external page.

Here is the code I have so far:

<?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, 开发者_JAVA百科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);
 }
?>


Seems like you're almost there.

You probably just want to die() or exit() after displaying the stuff returned fro cURL, so that your pre-POST version of contact.php doesn't get sent to the browser.

So you can just halt execution, or structure your code with conditionals so either the "display a form" stuff happens, or the "do some curly post and display the results" stuff happens, but not both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜