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.