I have created an API that is used on several sites. The client sites call the server with either file_get_contents or curl and accesses a url of ours like “http://www.myserver.com/api/record.php?some=thing“. The client makes the call and then waits for the API to respond before processing the rest of the page. However, some of the time no output from the API is needed by the client.
I’m wondering if there’s a way for the server to output something so that the client doesn’t wait for it while the server process is executing what it needs to do. Something like ‘page done’, so the client doesn’t wait for it to load anymore, but the server script is still running for a second while it inserts database entries.
I’ve read that the process control functions are for CLI use only and should not be used in a web server environment, so spawning a new process is out of the question.
Any suggestions? I know making the call asynchronously with javascript would work, but we’ve found PHP accesses to be must more reliable than JS.
Thanks so much!
The connection between your script and your api is the tricky thing; you must terminate it for the curl or file_get_contents to return. (you may look into the asynchronous curl_multi_* functions or do your own socket handling like artefacto suggested)
One option would be to put the backend handling in a backgrounded process, like so:
where sleep is just for the proof of concept. It lives happily on, after the connection is finished. The downside is that if you have any fd’s connected, the child process will die with the parent, so you must shove the data in through commandline or $env (5th arg to proc_open)