curl post form and redirect

Posted on 2007-10-19
Last Modified: 2011-07-16

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?

Question by:gothamww
    LVL 20

    Accepted Solution

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

    Author Closing Comment

    works very well, thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now