问题
Can you send me in the right direction for writing a Wait function in PHP? I'm using procedural code style.
The scenario is as following:
- First, my webapp sends data to a server, and then it should pause the execution on the sending client (the webapp).
- Second, the server receives the data and does something to that data, and it sends the processed data back to the sender of step 1 (the webapp).
- Third, the sender of step 1 (the webap) receives the processed data from the server and checks it whether it's true.
I need guidance in writing the wait and resume function.
回答1:
This defentely sounds like you need to use AJAX. jQuery gives you some comfortable ways to solve your Problem.
$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
This is a code example from https://api.jquery.com/jQuery.post/. You might have a look at https://api.jquery.com/jQuery.ajax/.
This AJAX function calls the script test.php with the given parameters as POST variables. You will recieve an answer (data), which you can use in your JS afterwards. I think the done() function does exactly what you are asking for. To have some consistency between the languages you could work with JSON objects.
You cannot actually stop PHP from executing. Thats why you will use a certain file which handles your request.
回答2:
The people here said that Ajax is needed to do this. This is true. But, it's not the only option available. Using cURL is an option too, and having CURLOPT_RETURNTRANSFER (which returns the value after the transfer) set it does what I needed to do:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
来源:https://stackoverflow.com/questions/22512083/wait-function-that-receives-data-in-php