Is it possible to send POST data with a PHP redirect?

前端 未结 10 2130
一个人的身影
一个人的身影 2021-02-14 05:04

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 w

相关标签:
10条回答
  • 2021-02-14 05:25

    it's not possibile using PHP.

    But you can use this tweak, In http://example.com/script.php you can write some javascript auto submit code with the checksum. E.g.

    <body onload="document.frm1.submit()">   
    <form method="POST" action="https://gatewaysite.com" name="frm1">
      <input name="checksum" value="yourchecksumvalue" />
      <input name="variable2" />
    </form>
    </body>
    
    0 讨论(0)
  • 2021-02-14 05:28

    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.

    0 讨论(0)
  • 2021-02-14 05:30

    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

    0 讨论(0)
  • 2021-02-14 05:32

    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

    0 讨论(0)
  • 2021-02-14 05:34

    you are only can post the values at the time of the form submission

    action = "path/filename.php";
    

    in this form you can post the form values.

    if you want to redirect the file at the time you can able to pass the values via the URL

    header('Location: url/filename.php?param1=      ');
    

    try this for a local file values which you want to post.

    first you need to assign the values from the form, then you relocate header with the variables. that's may be the right solution from my side. thank you.

    0 讨论(0)
  • 2021-02-14 05:36

    It seems to me that you're trying to do a redirect from one page to another, while sending along POST data. I'm afraid that's not possible with PHP.

    The simplest workaround I can think of, is using cURL to (1) forward your $_POST data to another server, (2) receive the result from that server and then (3) do a simple redirect to another page. While it requires a total of 3 HTTP requests (a bit much for such a simple problem), it does more or less achieve what you're trying to achieve.


    Let me break this approach down to the three steps you mentioned in your question :

    For step 1, any HTML form would do, as long as it has a "method" attribute with the value "post". If you want to submit the form without using JavaScript, just make sure it has a "submit" button. That will submit your data to the page defined in the "action" attribute of your form when the submit button is pressed :

    Example HTML :

    <form action="action_page.php" method="post">
        First name:<br />
        <input type="text" name="firstname" value="Mickey"><br />
    
        Last name:<br />
        <input type="text" name="lastname" value="Mouse" /><br /><br />
    
        <input type="submit" value="Submit" />
    </form>
    

    For step 2, you'll need to create a page with a name identical to the value of the "action" attribute of your form. In the case of our example, that would be action_page.php. Here, you check if $_POST contains any values for the form fields you submitted. In the case or our example, you could so something like this :

    if (isset($_POST['firstname']) && isset($_POST['lastname'])) {
        // Do the stuff you want to do on your own server here!
    } else {
        // Do nothing here!
    }
    

    For step 3, you'll need to make a second POST request, using the $_POST data you just received from your HTML form. The simplest way to do that, would be using cURL. Then, you redirect your user to a second page, depending on the output of your second POST request.

    To improve the readability of the example code, I put all code related to the cURL-request into a function :

    function forwardRequest($url, $data) {
        $result = [
            'status' => NULL,
            'last_url' => NULL,
            'response' => NULL
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $result['response'] = curl_exec($ch);
        $result['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        curl_close ($ch);
        return $result;
    }
    
    if (isset($_POST['firstname']) && isset($_POST['lastname'])) {
        // Do the stuff you want to do on your own server here!
    
        $result = forwardRequest("http://www.example.com/processdata.php", $_POST);
    
        if ($result['status'] === 200) {
            header('Location: http://www.example.com/ok.php');
        } else {
            header('Location: http://www.example.com/error.php');
        }
    } else {
        header('Location: http://www.example.com/youshouldnotbehere.php');
    }
    

    Note 1 :

    I'm afraid there is no generic way to determine whether or not everything goes well on an external system whenever data is submitted to that system.

    In my example code, I'm using the HTTP status code of the response received by your cURL request. This solution only works whenever the server returns a status code that is not equal to 200 if something goes wrong on that server. You will need to run some tests to see if this approach is sufficient for your use case.

    If you can't use the status code to determine whether everything went well or not, consider checking the last effective URL or look for clues in your response header & your response text.


    Note 2 :

    Instead of forwarding your user after your cURL request has been processed, you might just want to show the response text from the server to your user. If this result contains a lot of junk you don't need, consider parsing the result using DOMDocument::loadHTML or third party library (like Masterminds/html5-php or PHPPowertools/DOM-Query).

    You could also just write your own custom response text.

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