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:
a html form is submitted to a php script
the php script does some processing on the data and then submits data via POST request to an external website
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?
Its not possible,beacuase the page loaded entirely,there is a chance with empty data with POST request,instead of using this create Sessions when ever required,and after usage of that session just destroy the session. And the second one is usage of CURL .
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 :
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 :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 :
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.
Long story short: no, it's not possibile using PHP.
It's the only way to do it.
The question is a possible duplicate of PHP Redirect with POST data.
As a suicide solution, you may consider to use HTTP 307 - Temporary Redirect.
From the HTTP/1.1 documentation linked above:
My bold simply emphasizes that the redirect without the user confirmation actually depends on the browser's implementation of the protocol.
The redirect can be implemented in the PHP script which receives the POST request as follows*:
Basic usage Example:
page1.html - the page which contains the form
page2.php - the page which processes your POST data before redirect
page3.php the page to which the POST data has to be redirected (i.e. the external site)
* don't try this at home!
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.you are only can post the values at the time of the form submission
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
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.
If you are the owner of both websites, then you can just include the sites in each .htacess file, that will remain encypted.
The qualifications for this is SSL CA on both sites.
in your .htaccess, you do this by: this added in site#1's htaccess:
this added in site#2's htaccess:
Then you can use a standard html form on site#1 like: