[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4321
  • Last Modified:

curl post form and redirect


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?

1 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 (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">
gothamwwAuthor Commented:
works very well, thanks.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now