Search code examples
phpcodeigniterresponse

CodeIgniter RESTful, async / background process


I'm using codeIgniter RESTful API (https://github.com/philsturgeon/codeigniter-restserver) that return information (json format) to my android/iphone app.

There are an operation where i send some values, if it is everything OK i return 200 code as response.

Now, i want to add a new operation at the same method: send notifications of this modifications with APNS (Apple Push Notificacion Service) and GCM (Google Cloud Messaging).

It works well when i have to send no more than 3-5 notifications, the problem is APNS, because i have to send this messages one by one and it takes a long time, so my apps recieves a timeout exception (all the notifications are sent but the user get the Error Connection...)

Can i send the 200 code response and then continue sending this notifications? (Something like this...)

function my_update_method_post(){
   //....GET my POST values
   update($data);
   $this->response(array('result'=>1),200));


   //Send Notifications
   ....
}

Thanks in advance...


Solution

  • I found a solution that works perfect for me because i don't expect any result value. If notification can't be send...i log it in my database.

    This is the function that i use to send "async" request (yes, This is not an asynchronous request, but it works how i'm looking for)

    function curl_post_async($url, $params)
    {
        $post_string = http_build_query($params);
        $parts=parse_url($url);
    
        $fp = fsockopen($parts['host'],
            isset($parts['port'])?$parts['port']:80,
            $errno, $errstr, 30);
    
        if(!$fp)
        {
            //Perform whatever logging you want to have happen b/c this call failed!    
        }
        $out = "POST ".$parts['path']." HTTP/1.1\r\n";
        $out.= "Host: ".$parts['host']."\r\n";
        $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out.= "Content-Length: ".strlen($post_string)."\r\n";
        $out.= "Connection: Close\r\n\r\n";
        if (isset($post_string)) $out.= $post_string;
    
        fwrite($fp, $out);
        fclose($fp);
    }