2013-02-11 31 views
0

我目前有一个自定义购物车直接发送到2Checkout,并需要使用不同的付款方式相同的形式。PHP重定向到其他网站与POST变量

要做到这一点,我需要先将表格提交给我自己的服务器,然后将服务器重定向到选定的支付网关或我网站上的一个页面,其中详细介绍了其他支付选项。

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

我当前的代码不工作的用户不离开页面,他们需要......

//$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); 
+0

http://stackoverflow.com/questions/3045097/php-redirect-and-send-data-via-post – mithunsatheesh 2013-02-11 13:11:04

+0

你应该有一个表格,并提交它的值与你有。另一种方式是反对w3c规则,因为用户在他不知情的情况下被重定向。 – mithunsatheesh 2013-02-11 13:12:06

回答

1

一般来说,当你不得不再次从PHP POST给自己,然后张贴到新的位置,是你需要一个好的指针回去看看你的应用程序的结构。

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

第1步 - 信息
有您目前拥有的形式,让它后,你会在那一刻。

第2步 - 选择网关
拥有所有的$ _ POST变量保存在网页上的隐藏字段,并且有网关的列表,其中用户可以选择。当你点击其中一个,你可以可以发送用户到确认页面。

第3步 - 确认页
该网页的主要目的,是为用户检查自己输入的数据,并确认他想要购买的物品。

这个页面的第二个目的是把你拥有的所有数据放到隐藏的输入中(再次),但是这次你知道在哪里发送数据,从而知道哪些格式/命名字段应该有。


这是说,没有什么是从实际发布数据形式的PHP阻碍你,但正如我所说,这是一个很好的指针,你的应用程序的结构不再有意义。

+0

谢谢,我认为你是正确的,我只是想减少用户必须做的点击次数。 – trvo 2013-02-11 13:54:32

0
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); 
      } 
+0

谢谢rohitarora,我纠正了这一点,但它仍然没有重定向... – trvo 2013-02-11 13:00:55

+0

更新了我的答案希望它会帮助 – rohitarora 2013-02-11 13:04:44

+0

禁用标题,如果你不想 – rohitarora 2013-02-11 13:08:26

相关问题