Is it possible to send POST data with a PHP redire

2019-03-27 17:49发布

Update: This is NOT a duplicate of How do I send a POST request with PHP?. The solutions there don't work for me, they just output the result of the request, I don't want to do this, I want to send the user to the external site, it is secure website for credit card payment processing.

Update 2: I've added a diagram below to try and clearly explain what I'm trying to do

Here's what I'm trying to do:

  1. a html form is submitted to a php script

  2. the php script does some processing on the data and then submits data via POST request to an external website

  3. I do not want to receive the result of the POST request in my PHP script, instead I just want to send the user to the external site where they see the result of the POST

I'm thinking curl is not suitable for this task as I'm not interested in receiving the result of the request back, I just want to send the user to the next site just as if they submitted the form directly.

To put this another way, I want to submit a form to an external website but I want to submit it to my own script first so I can process the data and send email notifications and then submit onwards to the external website with some new calculated data added.

One way I can think of doing this is to output the new form output as hidden fields along with javascript to auto-submit the form but there has got to be a more straightforward, robust way to do it without relying on javascript. Maybe by manipulating headers, or maybe there is already a php function to simply submit a post request and redirect to the result?

What I'm trying to do

9条回答
Rolldiameter
2楼-- · 2019-03-27 18:34

Short answer :

  • POST to external server
  • parse response
  • do not echo out anything
  • use header('Location: url?param=res') ;
  • exit ; //this is kind of important

I do not want to receive the result of the POST request in my PHP script, instead I just want to send the user to the external site where they see the result of the POST

If you are sending a POST request from a PHP script, then the response will ALWAYS come back to the same PHP script. I think what you want is NOT to send this the response back to the user, but instead redirect the user to a another URL. For which you make sure you are doing.

header('Location: url?param=res') ;
exit ;

and not sending anything to the client before the header() call

查看更多
神经病院院长
3楼-- · 2019-03-27 18:35

I have one solution for you that will work either 99% of the time or 100% of the time depending on how you implement it. All you need to do is process the form with AJAX. I know you want a Javascript free way of doing this but it is the simplest way. If you use the jQuery library the documentation and SO answers are so easy to follow you wound not have to write much code. Just modify some. Here is the concept:

1) Page loads as normal with the forms action pointing to the payment processing website. Since you are using Javascript (jQuery) to submit the form you would have replaced the submit buttons html like so:

<input type="button" value="Check Out" onclick="NameofAjaxFunctionHere();"

2) Swap out the forms action value. Before the actual AJAX code that will send the form behind the scenes to be processed, use something similar to this code to change the action to your own PHP page:

 $('#formIdHere').attr('action', 'yourPHPpageAndPathHere.php');

This code is for jQuery but it should be easy to change to plain JavaScript if you need. Now also make sure your AJAX code does not reset the form. Many copy and past codes kindly do this for you.

3) (Optional) At this stage the form has been sent to your PHP page and you are done doing what you need to with it. You now have two options:

  • Do not wait for a response and just go on to number 4.
  • Wait for a response and react accordingly + change data if needed

If you need to make sure the data was correct or need to alter some of it do that now. If the forms data fails your process (altered data for example) send back a FALSE and move on to number 4. If you need to change things that is a simple process. Send back all the data that needs updating in an array and loop through it in the same AJAX code that started this process. Using Javascript you can change the forms values.

[Code missing because I think you can figure this out if you need to do that. If not comment and when I have time I will post an example]

4) Swap back the forms action. Your AJAX processing is done. Traditionally it wipes the form and displays a message. If you choose the option from above that sends a FALSE to stop submission in case there was bad data, just return false from the AJAX script now (maybe display something before that return saying the form was not sent).

 $('#formIdHere').attr('action', 'paymentUrlHere');

Now that the payment URL is back in place and any data was updated if you need to go that route, submit the form like it normally should (or would) be. IN jQuery this can be as simple as:

 $('#formIdHere').submit();

And I think that covers it. At this point the user is out of your hands and was sent to the payment processing website with a normal POST. You even had the chance to alter data from the form if needed.

OH PS - Here is what I meant about the 99% and 100% way at the start of this post. 100% is the example I gave you here. No one can send the form without making it to your own PHP page first. The 99% solution would be to but the normal submit html in the page like so:

<input type="submit" ...>

Then using Javascript stop the default submit action and follow my steps 1-4. That way if someone disables javascript (or is on a device that blocks it) the form still sends to the payment processor at least. Up to you what you want to do.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-27 18:39

You have excellent plan in you description. Why you just don't generate new POST request to 3 page with edited data on your page 2 after formatting? You can test your new POST requst with Chrome extension Postman

查看更多
登录 后发表回答