PHP sends a POST request to a separate thread and forgets

My applications process orders, and after the process, I would like to send the reservation data depending on which of my application partners made the reservation, so they can store a link to the reservation, And without deduction for other processing that must be performed my application.

I thought about how to send a message to a partner, and my decision would be to send a cURL POST request depending on which of my partners makes the reservation (in addition to answering my question, maybe someone has a better solution than this? )

Each partner will have a specific URL that will be configured to receive this POST request and save the reservation information that we send.

PROBLEM: If we try to send this POST request and their web server will be slow or down, we can wait too long to receive a response, which, in turn, will delay the confirmation of booking the actual user using our service.

PERFECT SOLUTION: I would like to send this PHP cURL request to another thread so that we can continue our fun and confirm the reservation. If there is a delay in another thread, this will not stop us.

Other solutions I reviewed:

  • Calling an external script (e.g. written in python) to send this request. I read that using exec () can be very resource intensive. We have many orders, so we ship many of these orders. Therefore, ideally, we need something resource conscience.
  • Using sockets. I am not familiar with their configuration, and I am worried that our socket server is down. It also looks like a maintenance mission. Maybe I'm wrong?
  • Using a service like Pusher , which is actually a socket service. The disadvantage is that if the listener skips the message, he will never receive it again. For instance. partner will skip saving this reservation.

It would be great to get some feedback on what I am trying to do here, especially from those who need a solution for the same situation that I am in. Thanks for any help!

+6
source share
2 answers

So, you are creating an API in PHP that other clients consume. Here is what I would suggest:

  • Let your customers contact you using the POST / GET method; instead of you, as an API server trying to pass data to your clients. this is a much better approach because it frees you from the fact that the client server is running, slow or something else. Therefore, when they send you a request, it means that they are fully capable of processing the response.

  • use an HTTP persistent connection: in apache, its name keep-alive sets its value to high, so clients can reuse an existing connection and therefore reduce the delay.

  • For multiprocessing in php take a look at Getting multiprocessing . Basically, there is a pcntl_fork() function that allows you to develop a process and create a new child process for multiprocessing.

  • Implementing a background job based on redis or something similar. the idea is that all lengthy work tasks fall into the background job queue, and then an employee is created for each task, so these tasks are performed through multiprocessor processing. Working PHP with Redis and Solo

Hope this helps

+2
source

What about a separate script passing through cron?
It is likely that a long delay will be required to obtain final confirmation (only the ability to send external requests every minute), but it will allow the user interface to simply store information in the queue and continue, and then the scheduled task can process it later.
The queue processor could check a valid response from an external service and try again, if necessary, without user interface support.

For more timely processing, you can create a daemon that checks the queue for records processed with less delay than is possible in cron. PEAR has a System_Daemon package that can help create a daemon in PHP.

+1
source

All Articles