0
I have a simple virtual shop system, where the customer places an order for certain products and the system records this, at the moment the customer logs the order the system sends an email message and also communicates with an API by passing the order data. The problem is that this whole process is taking too long to run so that I can then give a successful response to the customer. The act of sending the email and communicating with the API are not required for me to provide a successful response to the customer.
Having explained my situation, I wanted the system to be able to record the client’s order and immediately return the successful response, and subsequently carry out the actions of sending the email and communicating with the API.
I had two ideas, but I couldn’t find a solution for them:
1 - The first idea would be to return the answer to the client and continue with the rest of the script without my page getting stuck with these actions. It turns out that currently my php script only returns the message to the client when it finishes all the tasks (send email and API).
Ex: Client makes order->System registers order->System responds client [End of communication with client]->System sends e-mail ->System communicates API
2 - The second alternative was to create something similar to threads, but since it is a Webapp my Apache server does not support pthreads.
I would like to know if there is any solution to this problem.
PHP code I’m using:
// Funcao registra pedido
$resultado = registraPedido($_POST);
if ($resultado) {
//Funcao envia e-mail
enviaEmail($resultado);
//Funcao comunica API
comunicaAPI($resultado);
echo 'SUCESSO';
} else {
echo 'ERRO';
}
@Marcelobonifazio - I don’t need the email to confirm the request, it served only as an extra, so the act of registering the request does not necessarily need this step, it can be made after the answer given to the customer.
– Ruhan O.B
It turns out that the call functions of the email and the communication with the API should be made through PHP by security criteria. In this case, I do an Ajax call to a "request.php" script for example, and this records the request, calls the email function and the API function. I wanted to answer the AJAX call and continue running the other function in PHP without the page having to wait for the end of the script
– Ruhan O.B
The call I make by ajax, however it cannot be async because I need confirmation that the record has been saved
– Ruhan O.B
As I said before, I cannot call the email and API from Ajax for project security reasons, so it would have to be some alternative on the server side. Is there any way to force the script response to the client and still continue running the script without crashing the Ajax call?
– Ruhan O.B
And if after the reply of
AJAX
, you make another requestAJAX
to perform the other functions? For example: the customer made the order and returned a successful response, and then descends an order confirmation box and the hr he click onOK
, you by wayAJAX
have the other two functions carried out?– Max Rogério
that function
comunicaAPI
should follow the same logic of async?registraPedido
is the only operation that can’t be async from what I understand, right?– MarceloBoni
Correct, the order log is the main one, after it the success message should be returned to the customer, already the mail and communicationAPI should be async not to lock the answer. I tried using this solution http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response but it did not work.
– Ruhan O.B
I will give a better thought to your problem, try to create a minimal example here
– MarceloBoni
@Ruhandeoliveirabaiense your PHP is running on which Operating System, GNU/Linux?
– Fernando Mertins
The application is running on a Linux hosting, with Apache, but as it is a reseller, I only have access to the settings by cPanel
– Ruhan O.B