Redirect with POST data

前端 未结 4 1947
野的像风
野的像风 2020-12-19 18:55

I am working on a shopping cart with several payment options using different APIs.

The flow I want is that the user chooses which payment option to use and the accor

相关标签:
4条回答
  • 2020-12-19 19:01

    You can't send a traditional Location: header to the HTTP client to do a redirect and include POST data. What you could do instead is:

    • use an AJAX request to fetch the correct target URL
    • update the form action with that URL via JavaScript (in the AJAX callback function)
    • submit the form
    0 讨论(0)
  • 2020-12-19 19:09

    Try this

            $context = stream_context_create(array(
            'http' => array(
              'method'  => 'POST',
              'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
              'content' => http_build_query($_POST)
            ),
          )); 
    
        echo file_get_contents($url, false, $context);
    
    0 讨论(0)
  • 2020-12-19 19:18

    Easiest thing to do is have a form submit handler in JavaScript code that will make an AJAX POST request to save the customer-related info.

    • The payment form's action should point to the payment gateway
    • Attach a JavaScript on submit event to the payment form
    • In the submit handler, gather all of the customer info (name, address, etc) and make an AJAX POST to your server.
    • Make sure that the submit handler returns false so that the POST does not submit to the payment gateway just yet
    • Once the AJAX POST succeeds submit the payment form using JavaScript.

    This also saves you because you can control what parameters to send to your server (the customer's credit card / CVV numbers should not be sent to your server unless you are certified to handle them).

    0 讨论(0)
  • 2020-12-19 19:18

    If you have the PECL http extension installed (it'll show up in phpinfo() as pecl_http) you can create an HTTP POST request and spit out the results after the following pattern:

    // your DB code above here
    $pay_request = new HttpRequest($remote_url, HTTP_METH_POST);
    $pay_request->addPostFields($_POST);
    $pay_request->send()->send();
    

    Last line's not a typo - the request returns a response, and then the response gets dumped in the output buffer.

    0 讨论(0)
提交回复
热议问题