Saturday 27 June 2015

asynchronous call to php function

A trick to make a call to a laggy function through curl to avoid time penalty on main call. Useful on emails sending.
/**
 *  make asyncr calls to functions
 *
 * @param string $functionname
 * @param array $params
 * @return boolean
 */
function asincFunctionCall($functionname,$params){
    global $app;
    $time_start = microtime(true);
    $app->log->info("asinc call init");
    $params['pass']="validpass123141417093412341";
    $url = "http://".$_SERVER['HTTP_HOST']."/asincfunct/".$functionname."/";
    //$url = "localhost:8888/asincfunct/".$functionname."/";
    $url .="?".http_build_query($params);
    exec("curl -X GET -H 'Content-Type: application/json' -d '' '".$url."' > /dev/null 2>&1 &", $output, $exit);
    $time_end = microtime(true);
    $app->log->info("asinc call end: ".($time_end - $time_start));
    return true;
}     

0 comments:

Post a Comment