自动提交表单 (cURL)
我正在编写一个表单以将数据发布到贝宝,这很好用,我创建了带有隐藏字段的表单,然后有一个提交按钮将所有内容提交给贝宝.
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.
我不想为此使用 javascript.
I dont want to use javascript for this.
我目前的方法是使用 cURL,表单回发到当前页面,然后我检查 $_POST 数据,执行我的其他命令,例如更新购物车的状态,然后创建一个 curl 命令,然后发布表单数据到paypal.现在,它已成功发布,但浏览器没有转到 paypal.
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.
起初我只是在字符串中检索结果,然后将其回显出来,我可以看到帖子成功,然后我将 CURLOPT_FOLLOWLOCATION 设置为 1 假设这会让浏览器转到 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.
为此使用 cURL 是否正确?如果是这样,我该如何解决这个问题?我想为此远离 javascript,因为只有启用了 javascript 的用户才会更新他们的购物车.
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.
我用于 curl 的代码如下,post_data 是我创建的一个数组,然后将键值对读入 post_string.
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);
推荐答案
也许尝试将 CURL 与 header 语句结合起来
Perhaps try to combine CURL with a header statement
<?php
header ("Location: https://www.paypal.com/");
这可能会起作用,具体取决于浏览器是否需要将任何状态(会话)信息传递给 paypal.此会话信息收集在您的 CURL 调用中,但遗憾的是无法将其传递给浏览器.所以如果你需要维护状态,那么这行不通.
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 操作在服务器端.它不会影响您的浏览器.CURLOPT_FOLLOWLOCATION 只是指示 CURL 遵守,例如,当贝宝向它发送一个像上面提到的标头时.指示浏览器执行某些操作的唯一方法是使用上述 HTTP 标头或 HTML 响应中的某种元标记进行响应,例如
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/">
作为替代方案,paypal 是否不提供重定向功能,该功能会在交易完成后重定向回您的服务器.如果是这样,那么只需在此回调期间更改卡状态并跳过整个卷曲过程?
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?
相关文章