curl post form and redirect


I'm posting a form to 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, 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 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("");
      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'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 at the same time?

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 (")

What you need to do is

$aQueryStrs = array();
      print "<form name='myform' action='' 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">
gothamwwAuthor Commented:
works very well, thanks.
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.