开发者

Auto Submitting a form (cURL)

开发者 https://www.devze.com 2022-12-25 18:17 出处:网络
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.

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 aw开发者_Go百科ay 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?


After receiving the form and doing what you need to do you should present another form with only a button (all other data in hidden fields) for the user to click and finally head off to paypal.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号