PDA

View Full Version : cURL fun


Sawtooth500
07-14-2009, 11:20 PM
So I have this nice and big form that collects data from users, the data is simply emailed. Also, when someone submits the form payment has to be submitted, and I'm just using one of those PayPal buy it now buttons. The code for the button is:


<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="6803567">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>


Now I can't use that as my actual form code because there is much much more data I need to collect and if the action goes to paypal I won't get the data - so my form goes to an intermediate processing page and then that page needs to submit post data directly to paypal. That is where cURL comes in:


$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "cmd=_s-xclick&hosted_button_id=6803567");
curl_exec ($ch);
curl_close ($ch);
header("Location: https://www.paypal.com/cgi-bin/webscr");


As you can see from the paypal HTML code, there are only two POST fields that need to be sent. So I need cURL to redirect and send those two post fields. What is wrong with my PHP code for cURL? Right now it redirects to paypal, but goes to the paypal home page, which is what happens when that paypal URL gets typed in with no post values.

paul_norman_81
07-15-2009, 06:30 AM
Okay, this is one of the most frustrating things about Paypal - the limited data field transport. Basically to answer your question you cannot use cURL for this. It will submit your post to that page, but the user is redirected completely separately from it, i.e. it cURL heads off down one road and the user down another (to the same location), but without the post data. You MUST use the form as it was intended.

You can get around this though, Paypal allow a custom field(s) for your data that is passed back to you upon success or failure and at this point you can use the data. This does mean that the Paypal form cannot directly ask the user questions (as they won't be in this special field!) and that you will have to have a separate form with this information taken prior to submission that populates this field. Lame I know.

Sawtooth500
07-15-2009, 10:23 AM
Yeah, I wish paypal were a bit more flexible too...