0

Not really sure if this is possible, but i am trying to call multiple apis but I don't want user to wait for the call to finish. Once they submit a form, I would like them to go to the next page(Next function in the controller) and make asynchronous call to the apis. FYI I am using codeigniter framework.

Asif Alamgir
  • 1,454
  • 1
  • 16
  • 41
  • I dont think i can use ajax. If i am using ajax, then i will have to be atleast be on that page while that ajax call finishes. – Asif Alamgir Feb 05 '15 at 16:23
  • so, what you want is call to different functions while user navigate –  Feb 05 '15 at 16:25
  • Best practice is to resolve the request first, and then take steps(what you are doing actually). For example, if a user is submitting some form, and after submission you redirect him to other page without resolving whether the request was successful or not, that's not good actually. The form might have validation error, and for this case, you should not redirect him from the form page, but you are. I hope it helps. – Anindya Dhruba Feb 05 '15 at 16:25
  • another thing you can do is work with partial views. You're on the same page always, just actualize the content –  Feb 05 '15 at 16:29
  • If you're looking for acync PHP you may also look at http://reactphp.org. Pretty new, but could be what will help you. – seangates Feb 06 '15 at 03:54

3 Answers3

1

Something like this should work.

Basically you want to call the API upon submitting.

The API call will have code which guarantees that the script finishes executing even if the client is no longer listening for a response.

jQuery AJAX

$('form').on('submit', function(){
    // call the API
    $.get('www.yoursite.com/api_caller/callApi');
});

API Call

class Api_caller extends CI_Controller {

    public function __construct(){
        // Even if you navigate away from page then
        // this script will finish
        ini_set('ignore_user_abort', '1');
    }

    public function callApi ($name = '') {
        // Call your API(s) with CURL
    }

}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
1

PHP scripts by themselves are single threaded. There are ways to "fork" child processes in PHP using pcntl_fork function. so far I know, this only really works well with the CLI sapi.

Forking a process will result in the request being cloned into an exact replica, though with it's own address space. Both the parent and the child (forked) process will be exactly the same up until the moment of the fork, e.g.: any variables up to that point will be exactly the same in both processes. After forking, changing a variable's value in one process doesn't affect the other process though

you can also try curl_multi_init. it allows the processing of multiple cURL handles asynchronously.

another solution could be pthreads. upon thread completion, process the second thread's result in the original thread. Threading really gains an edge over multiprocessing if it's necessary to transfer data between threads or to keep the execution of several steps in both threads in sync, via synchronized(), notify() and wait().

sas
  • 2,563
  • 1
  • 20
  • 28
  • I like the idea of using fork. When my child process finishes curl calls, how can i kill the process so that it doesnt affect my memory. Will exit() kill the process completely? – Asif Alamgir Feb 05 '15 at 16:52
  • On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised. – sas Feb 05 '15 at 16:59
  • i don't want parent to wait for child process to complete. – Asif Alamgir Feb 05 '15 at 17:24
  • you can do it. please take a look here, you will get a very good understanding http://stackoverflow.com/questions/136429/dont-wait-for-the-process-to-exit – sas Feb 06 '15 at 16:02
0

I think the solution would be to make several Ajax calls on the next page. These calls do not need to go to directly to the APIs but to multiple proxy PHP Scripts on your server which call the APIs .

Sebastian Viereck
  • 5,455
  • 53
  • 53