title: Get a list of active printers from within PHP
body: <p>I'm using PHP to get an active list of printers connected to my Xubuntu linux machine and then create an array that shows only the printers which are online. I wanted to use this array to determine programmatically, which printer can be used to print.</p>
  
  <p>To accomplish this, I call from within PHP</p>
  
  <pre><code>lpstat -p -d
  </code></pre>
  
  <p>This gives the response:</p>
  
  <pre><code>printer DYMO-LabelWriter-400-Turbo-1 disabled since Fri 26 Jul 2013 10:45:50 AM UTC -
      Unable to send data to printer.
  printer DYMO-LabelWriter-400-Turbo-2 is idle.  enabled since Mon 04 Nov 2013 04:30:57 AM UTC
  printer Samsung-SCX-4200 is idle.  enabled since Wed 10 Jul 2013 11:30:49 AM UTC
  system default destination: DYMO-LabelWriter-400-Turbo-1
  </code></pre>
  
  <p>However, this list is incorrect. At the time of running the call, none of the above printers are actually connected. Is there a way to probe, if the printer can be used at all?</p>
  
  <p>EDIT:
  Here's the code I use:</p>
  
  <pre><code>function getActivePrinters() {
  
      $o = shell_exec("lpstat -d -p");
      $res = explode("\n", $o);
      $i = 0;
      foreach ($res as $r) {
          $active = 0;
          if (strpos($r, "printer") !== FALSE) {
              $r = str_replace("printer ", "", $r);
              if (strpos($r, "is idle") !== FALSE)
                  $active = 1;
  
              $r = explode(" ", $r);
  
              $printers[$i]['name'] = $r[0];
              $printers[$i]['active'] = $active;
              $i++;
          }
      }
      return $printers;
  }
  </code></pre>