title: WhatsApp Check Last Seen
body: <p>I am using this repo <a href="https://github.com/mgp25/Chat-API" rel="nofollow">https://github.com/mgp25/Chat-API</a> - I've got the register tool working, but what I want to do is check when a user was last online. </p>
  
  <p>I currently have this code, which logs me in but doesn't seem to check the persons status - any ideas how to get this working?</p>
  
  <pre><code>&lt;?php
  
  set_time_limit(10);
  require_once __DIR__.'/../src/whatsprot.class.php';
  require_once __DIR__.'/../src//events/MyEvents.php';
  
  //Change to your time zone
  date_default_timezone_set('Europe/Madrid');
  
  //######### DO NOT COMMIT THIS FILE WITH YOUR CREDENTIALS ###########
  ///////////////////////CONFIGURATION///////////////////////
  //////////////////////////////////////////////////////////
  $username = '447732XXXXXX';                      // Telephone number including the country code without '+' or '00'.
  $password = 'XXXXXXXXXXXX';     // Use registerTool.php or exampleRegister.php to obtain your password
  $nickname = 'JB';                          // This is the username (or nickname) displayed by WhatsApp clients.
  $target = "4477715XXXXX";                   // Destination telephone number including the country code without '+' or '00'.
  $debug = false;                                           // Set this to true, to see debug mode.
  ///////////////////////////////////////////////////////////
  
  function fgets_u($pStdn)
  {
      $pArr = [$pStdn];
  
      if (false === ($num_changed_streams = stream_select($pArr, $write = null, $except = null, 0))) {
          echo "\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n";
  
          return false;
      } elseif ($num_changed_streams &gt; 0) {
          return trim(fgets($pStdn, 1024));
      }
  }
  
  //This function only needed to show how eventmanager works.
  function onGetProfilePicture($from, $target, $type, $data)
  {   
  
      if ($type == 'preview') {
          $filename = 'preview_'.$target.'.jpg';
      } else {
          $filename = $target.'.jpg';
      }
  
      $filename = Constants::PICTURES_FOLDER.'/'.$filename;
  
      file_put_contents($filename, $data);
  
      echo '- Profile picture saved in '.Constants::PICTURES_FOLDER.'/'.$filename."\n";
  }
  
  function onPresenceAvailable($username, $from)
  {
      $dFrom = str_replace(['@s.whatsapp.net', '@g.us'], '', $from);
      echo "&lt;$dFrom is online&gt;\n\n";
  }
  
  function onPresenceUnavailable($username, $from, $last)
  {
      $dFrom = str_replace(['@s.whatsapp.net', '@g.us'], '', $from);
      echo "&lt;$dFrom is offline&gt; Last seen: $last seconds\n\n";
  }
  
  echo "[] Logging in as '$nickname' ($username)\n";
  //Create the whatsapp object and setup a connection.
  $w = new WhatsProt($username, $nickname, $debug);
  $w-&gt;connect();
  
  // Now loginWithPassword function sends Nickname and (Available) Presence
  $w-&gt;loginWithPassword($password);
  
  //Retrieve large profile picture. Output is in /src/php/pictures/ (you need to bind a function
  //to the event onProfilePicture so the script knows what to do.
  //$w-&gt;eventManager()-&gt;bind('onGetProfilePicture', 'onGetProfilePicture');
  //$w-&gt;sendGetProfilePicture($target, true);
  //$w-&gt;sendPresenceSubscription($target)
  $w-&gt;sendPresenceSubscription($target);
  
  
  
  ?&gt;
  </code></pre>