PHP 表单 POST 到外部 URL,并重定向到另一个 URL
因此,我想要实现的是拥有一个自发布 PHP 表单,POST 到外部页面(使用 CURL),该页面又重定向到另一个页面。目前,发生的情况是,一旦我单击表单(在 contact.php 中)上的“提交”,它将 POST 到自身(因为它是一个自我发布表单)。然后,该脚本使用 CURL 准备 POST 并执行该 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);
}
?>
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, 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);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你快到了。
您可能只想在显示从 cURL 返回的内容后 die() 或 exit() ,这样您的 contact.php 的 pre-POST 版本就不会发送到浏览器。
所以你可以停止执行,或者用条件构造你的代码,这样要么
“显示表单”的事情发生,或者“做一些卷曲的帖子并显示结果”的事情发生,但不是两者都发生。
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.