自动提交表单(cURL) [英] Auto Submitting a form (cURL)

查看:141
本文介绍了自动提交表单(cURL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写表单以将数据发布到paypal时,此操作正常,我创建了带有隐藏字段的表单,然后有一个提交按钮可将所有内容提交到paypal。



但是,当用户单击该按钮时,我想要做的更多,例如在数据库中更改他们的购物车状态。所以,我想要能够执行一些代码,当他们单击提交,然后将数据发布到paypal。



我不想使用javascript这个。 >

我的方法目前使用cURL,表单发回到当前页面,然后检查$ _POST数据,做我的其他命令,如更新购物车的状态,然后创建curl命令,并将表单数据发布到paypal。现在,它成功发布,但浏览器没有去paypal。



起初我只是retirieving结果在一个字符串,然后回声,我可以看到帖子是成功的,然后我设置CURLOPT_FOLLOWLOCATION为1,假设这将让浏览器开始PayPal,但它does not,它似乎做的是从paypal一些东西,并把它放在我的页面。



使用cURL是正确的吗?如果是这样,我如何绕过这个问题?



使用curl的代码如下,post_data是一个数组我创建了然后读取键值对到post_string。

  //设置cURL选项
curl_setopt($ curl_connection, CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($ curl_connection,CURLOPT_USERAGENT,
Mozilla / 4.0(compatible; MSIE 6.0; Windows NT 5.1));
curl_setopt($ curl_connection,CURLOPT_RETURNTRANSFER,false);
curl_setopt($ curl_connection,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ curl_connection,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ curl_connection,CURLOPT_MAXREDIRS,20);

//设置要发布的数据
curl_setopt($ curl_connection,CURLOPT_POSTFIELDS,$ post_string);

//执行请求
curl_exec($ curl_connection);


解决方案

也许尝试将CURL与头语句



 <?php 
header(Location:https://www.paypal.com/);

这可能取决于浏览器是否需要将任何状态(会话)信息传递到paypal。这个会话信息是在CURL调用中收集的,但是没有办法将它传递给浏览器。所以如果你需要维护状态,那么这将不工作。



Curl在服务器端运行。它不会影响您的浏览器。 CURLOPT_FOLLOWLOCATION只是指示CURL服从时,例如paypal发送它像上面提到的头。指示浏览器执行某些操作的唯一方法是使用上述HTTP标头或在HTML响应中使用某种元标记进行响应,例如

 < META HTTP-EQUIV =refreshCONTENT =10; URL = https://www.paypal.com /> 

另外,paypal不提供重定向函数,当事务完成时,重定向返回到您的服务器。如果是这样,那么只是在回调期间更改卡的状态,并跳过整个卷曲过程?


Im writing a form to post data off to paypal, and this works fine, i create the form with hidden fields and then have a submit button that submits everything to paypal.

However, when the user clicks that button, there is more that i want to do, for example change their cart status in the database. So, i want to be able to execute some code when they click submit and then post the data to paypal.

I dont want to use javascript for this.

My method at the moment is using cURL, the form posts back to the current page, i then check for $_POST data, do my other commands like updating the status of the cart, and then create a curl command, and post the form data to paypal. Now, it gets posted successfully, but the browser doesnt go off to paypal.

At first i was just retirieving the result in a string and then echoing it out and i could see that the post was successful, then i set CURLOPT_FOLLOWLOCATION to 1 assuming this would let the browser go off to paypal but it doesnt, what it seems to do is grab some stuff from paypal and put it on my page.

Is using cURL the right thing for this? and if so, how do i get round this problem? I want to stay away from javascript for this as only users with javascript enabled would have their cart updated.

The code im using for curl is below, the post_data is an array i created and then read key-value pairs into post_string.

//Set cURL Options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 20);

//Set Data to be Posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//Execute Request
curl_exec($curl_connection);

解决方案

Perhaps try to combine CURL with a header statement

<?php
header ("Location: https://www.paypal.com/"); 

This might work depending if the browser need to pass any state (session) info to paypal. This session info are gathered in your CURL call, but there is no way to pass that on to the browser unfortunately. So if you need to maintain the state, then this will not work.

Curl operates on the server side. It won't affect your browser. CURLOPT_FOLLOWLOCATION simply instructs CURL to obey when for instance paypal sends it a header like the one mentioned above. The only way to instruct the browser to do something is to respond with an HTTP header like above or with some kind of meta tag in your HTML response e.g.

<META HTTP-EQUIV="refresh" CONTENT="10;URL=https://www.paypal.com/">

As an alternative, does paypal not provide a redirect function that will, when the transaction is finished, redirect back to your server. If so then just change the card status during this callback and the skip the whole curl process?

这篇关于自动提交表单(cURL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆