【问题标题】:PHP redirect to another site with POST variables [duplicate]PHP使用POST变量重定向到另一个站点[重复]
【发布时间】:2013-02-11 12:41:30
【问题描述】:

我目前有一个直接发送到 2Checkout 的自定义购物车,并且需要对不同的付款方式使用相同的表单。

为此,我需要先将表单提交到我自己的服务器,然后将服务器重定向到所选的支付网关或我网站上详细说明其他支付选项的页面。

我将如何使用变量重定向用户作为 PHP(不是 javascript)中的 post 方法?

我当前的代码不起作用,因为用户没有离开页面,他们需要...

//$url = 'https://www.2checkout.com/checkout/spurchase';
$url = 'getpost.php';

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);  // RETURN THE CONTENTS OF THE CALL
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

【问题讨论】:

  • 您应该有一个表单并在加载时提交您拥有的值。另一种方法是违反 w3c 规则,因为用户在他不知情的情况下被重定向。

标签: php redirect post curl


【解决方案1】:

通常,当您必须向自己 POST,然后再次从 PHP POST 到新位置时,这是一个很好的指针,您需要返回并查看应用程序的结构。

更顺畅的解决方案可能是将支付流程分解为多个步骤。

第 1 步 - 信息
拥有您当前拥有的表单,并按照您现在的方式发布。

第 2 步 - 选择网关
将所有 $_POST 变量存储在页面上的隐藏字段中,并有一个网关列表,用户可以选择。当您点击其中之一时,您可以将用户发送到确认页面。

第 3 步 - 确认页面
此页面的主要目的是让用户查看他输入的数据,并确认他想购买该商品。

此页面的次要目的是获取您拥有的所有数据,并将它们放入隐藏输入(再次),但这次您知道将数据发送到哪里,从而知道字段应该具有哪种格式/命名.


话虽如此,没有什么能阻止您实际发布 php 表单的数据,但正如我所说,这是一个很好的指针,您的应用程序结构不再有意义。

【讨论】:

  • 谢谢,我认为你是对的,我只是在尝试减少用户必须做的点击次数。
【解决方案2】:
curl_setopt($ch,CURLOPT_POST, count($fields));

它接受布尔值,像这样使用它

curl_setopt($ch,CURLOPT_POST, TRUE);

这里有一个完美的 curl 功能,可以处理每一篇文章,获取 http https 登录和每件事。

curl_grab_page($url,$data)一样使用它,输入url url和数据就是它

function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")

            {
                if($login == 'true') {
                    $fp = fopen("cookie.txt", "w");
                    fclose($fp);
                }
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
                curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

                curl_setopt($ch, CURLOPT_TIMEOUT, 60);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                if ($proxystatus == 'true') {
                    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
                    curl_setopt($ch, CURLOPT_PROXY, $proxy);
                }
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

                if($secure=='true')
                {
                    curl_setopt($ch, CURLOPT_SSLVERSION,3);
                }

                curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );


                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_REFERER, $ref_url);
                curl_setopt($ch, CURLOPT_HEADER, TRUE);
                curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                ob_start();

                return curl_exec ($ch); // execute the curl command

                curl_getinfo($ch);
                ob_end_clean();
                curl_close ($ch);
                unset($ch);
            }

【讨论】:

  • 感谢 rohitarora,我已更正此问题,但仍无法重定向...
  • 更新了我的答案希望对您有所帮助
  • 如果你不想禁用标题
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多