PHP send POST request in separate thread and forget PHP send POST request in separate thread and forget curl curl

PHP send POST request in separate thread and forget


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


What about having a separate script running through cron?
It would likely require a greater delay on obtaining final confirmation (only being able to send external requests every minute), but would allow the user interface action to just store the information in a queue and continue, and then the scheduled task can process it later.
The queue processor would be able to check for a valid response from the external service and try again if necessary, without holding up the user interface.

For more timely processing, you could create a daemon that checks the queue for entries to process on a smaller delay than is possible with cron.PEAR has a System_Daemon package that can help with creating the daemon in PHP.