curl post form and redirect

Hi:

I'm posting a form to authorize.net for credit card payment.  I'd like to save some of the post variables in my own database before sending the form fields off to authorize.net, so I'm posting the form to my server, storing the variables I need to, and then trying to programmatically post the form.  It looks like I ought to be able to use cURL to do this, but I'm having trouble getting it to work the way I'd like it to.

I'm using this code:

     // store my own form values on my server here...
    ....some code to do that goes here....

    /// now post the form to authorize.net as if I hadn't intercepted it

      $aQueryStrs = array();
      foreach ($_POST as $key => $value) {
            $value = urlencode(stripslashes($value));
            $aQueryStrs[] .= "$key=$value";
      }
      $query_string = implode('&',$aQueryStrs);

      $Curl_Session = curl_init("https://secure.authorize.net/gateway/transact.dll");
      curl_setopt ($Curl_Session, CURLOPT_POST, 1);
      curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, $query_string);
      curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
      curl_exec ($Curl_Session);
      curl_close ($Curl_Session);

This posts the form and returns the HTML to my server.  What I'd really like to do is post the form and end up on authorize.net's server.   Without knowing too much about cURL it would seem that setting the FOLLOWLOCATION option to 1 would do this, but it doesn't.  The resulting HTML is returned to my server and the browser stays there - it doesn't redirect.  How can I post the form and redirect to authorize.net at the same time?

-Charlie
gothamwwAsked:
Who is Participating?
 
steelseth12Connect With a Mentor Commented:
curl returns the output to your page . It doesnt redirect to that page. The FOLLOWLOCATION option means that it will follow any headers sent from the page (https://secure.authorize.net/gateway/transact.dll")

What you need to do is

<?
$aQueryStrs = array();
      
      print "<form name='myform' action='https://secure.authorize.net/gateway/transact.dll' method='post'>";

      foreach ($_POST as $key => $value) {
           
                  $value = urlencode(stripslashes($value));
           
                  print "<input type='hidden' name='".$key."' value='".$value."'>";
                  
                  $aQueryStrs[] .= "$key=$value";
      }
        
        print "</form>";
        
        ?>
     
      <script language="javascript">
        
        document.myform.submit();
        
        </script>
0
 
gothamwwAuthor Commented:
works very well, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.