phpmultithreadingcurlasynchronous

PHP send POST request in separate thread and forget


My app processes bookings, and upon process I would like it to send the booking details to whichever of my app's partners made the booking, so they can store a reference to the booking, AND without hold up the other processing my app has to do.

I've thought about how to send a message to a partner, and my solution would be to send a cURL POST request to whichever of my partner's is making the booking (besides answering my question maybe someone has a better solution than this?).

Each partner would have a specific URL that they would setup to receive this POST request and store the booking information we send them.

THE PROBLEM: If we try send this POST request and their web server is slow or down then we may wait unnecessarily long to get a response, which in turn would delay the confirmation of the booking of the actual user making use of our service.

IDEAL SOLUTION: I would like to send this PHP cURL request in another thread so we can continue on our merry way and confirm the booking. If in the other thread there is a delay, this will not hold us up.

Other solutions I have considered are:

It would be great to get some feedback about what I'm trying to accomplish here, especially from someone that has been in need of a solution for the same kind of situation I'm in. Thanks for any help!


Solution

  • So essentially you are creating an API in PHP that other clients consume. here is what I would suggest:

    1. Let your clients make requests to you, via POST/GET method; instead of you as a API server trying to push data to your clients. that is much better approach because it frees you with things client's server down, slow or something else. So when they send you a request, it means they are fully capable for handling response.

    2. use HTTP persistent connection: in apache its called keep-alive set its value to high, so clients can reuse existing connection & thus reduced latency.

    3. For multiprocessing in php, have a look at Getting into multiprocessing. Basically, there is pcntl_fork() function which allows you to fork a process & create new child process for multiprocessing.

    4. Implement a background job que based on redis or something similar. idea is that all long running jobs gets dropped into the background job que & then a worker is spawned for each task so these jobs are executed via multiprocessing. PHP Workers with Redis & Solo

    Hope it helps