Scott Gray
Scott Gray

Reputation: 11

PHP cURL Post to and display of External Page

I am trying to rebuild a site that currently presents a form to the user, has the user click a button to post to a page that does some calcs based on the data entered, and has them hit another button to post to a payment vendor that presents a secure payment form utilizing the non-sensitive data entered.

I am trying to do the following. I have written a page that presents the form for customer data entry, they click a submit button, that posts to a php file on my site, I make the calculations needed, and then post to the site using Curl.

I have only used cURL in the past to post to a site, check for a good process status, and continue on. Can I use Curl to post to and then go to the other page in the browser, just like I do when I submit the form directly to it?

In Summary:

  1. Can I post to a form in Curl and have it act just as if I posted from HTML form with the action set to the external site url.

  2. If so, what I have I missed?

Here is the code from the cUrl call in the php file:

$fields = array(
    'x_invoice_num' => urlencode($x_invoice_num),
    'x_phone' => urlencode($x_phone),
    'x_email' => urlencode($x_email),
    'x_ship_to_address' => urlencode($x_ship_to_address),
    'x_first_name' => urlencode($x_first_name),
    'x_last_name' => urlencode($x_last_name),
    'x_address' => urlencode($x_address),
    'x_city' => urlencode($x_city),
    'x_state' => urlencode($x_state),
    'x_zip' => urlencode($x_zip),
    'x_amount' => urlencode($x_amount),
    'x_fp_hash' => urlencode($x_fp_hash),
    'x_fp_sequence' => urlencode($x_fp_sequence),
    'x_fp_timestamp' => urlencode($x_fp_timestamp),
    'x_login' => urlencode($x_login),
    'x_show_form' => urlencode($x_show_form),
    'x_description' => urlencode($x_description)
);

        //url-ify the data for the POST
       $fields_string="";
       foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
       rtrim($fields_string, '&');





       //set the url, number of POST vars, POST data

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $postURL);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       curl_setopt($ch, CURLOPT_HEADER, FALSE);
       curl_setopt($ch, CURLOPT_POST, TRUE);
       curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

       $headers = array();
   $headers[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
   $headers[] = "Accept-Language: en-us,en;q=0.5";
   $headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

       //execute post
       //curl_exec($ch);

       $curl_result = curl_exec($ch);
       $OK = strpos($curl_result, 'OK');
       $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);


       curl_close($ch);

Upvotes: 1

Views: 696

Answers (1)

MegaAppBear
MegaAppBear

Reputation: 1220

The idea of having the user redirected to the secure payment provider is that he/she will enter some sensitive payment info (CC, expiry, ...) to be processed. I don't think that you have this info, among your "$fields" array, to be able to POST directly to the provider.

What you want to look at is probably a callback from the provider's website once the payment is successful and complete.

Upvotes: 1

Related Questions