使用chat-API接收消息

I want to make an retrieve messages from WhatsApp server every 2 seconds. I am using this jQuery function :

          setInterval(function(){   
                $.post("recevie_message.php",{},function(data){

                       console.log(data);
                       $arr=JSON.parse(data);
                       $.each($arr,function(index,value){
                              console.log(value.body);
                              var templateResponse = Handlebars.compile( $("#message-response-template").html());
                              var contextResponse = {response:value.body};
                   $('.chat-history').append(templateResponse(contextResponse));

                        });
          },2000);

and the PHP code:

<?php
  include_once('checkUserSession.php');
  $w=$task->connectToServer();
 // header ('Content-Type: text/html; charset=UTF-8'); 
  $username= $task->getPhoneNumber();
  $password = $task->getWhatsappPassword();
  $msg = array();
  $i=0;
  function onMessage($mynumber, $from, $id, $type, $time, $name, $body)
{
   $GLOBALS['msg'][$GLOBALS['i']]=array("from"=>$from,"body"=>$body);
   $GLOBALS['task']->saveMessage($body,'f','t',$from);
   $GLOBALS['i']++;
}


      include_once('Chat-API-master/src/events/MyEvents.php');
      $events = new MyEvents($w);
      $w->eventManager()->bind("onGetMessage", "onMessage");

      $w->pollMessage();
      echo json_encode($msg);
      $w->disconnect();


      //$w->disconnect();

    ?>

The problem is that I am getting loginFaulierException at many calls (not all of them). Is there is a better way to receive messages or in other words how does the WhatsApp native app works?