Manuel Pear

importVendors()

importVendors() -- Import a manufacturers' file to the database or to an array

Synopsis

  1. <?php
  2. require_once 'Net/MAC.php
  3. ?> 

mixed importVendors (chaîne de caractères $file [, booléen $doReturn=FALSE])

Description

This method will parse a manufacturers' file, such as the one from http://anonsvn.wireshark.org/wireshark/trunk/manuf, containing a list of MAC address prefix-to-vendor relationships. If the $doReturn parameter is FALSE, then the data will be imported into the database defined by the factory of this class. However, if $doReturn is TRUE, then the return will be an associative array with the key being the MAC address prefix and the data being an associative array with the keys 'vendor' and 'description'.

Paramètres

  • chaîne de caractères $file - The filename or URL of the manufacturers' file to parse

  • chaîne de caractères $doReturn - If TRUE, an array will be returned, if FALSE, the data will be imported into the database.

Valeur retournée

mixed - If $doReturn is TRUE, the method will return an array. Otherwise, the method will return TRUE on success. A Net_MAC_Exception Exception object will be thrown on failure in either case.

Note

Cette fonction ne peut pas être appelée de façon statique.

This method can throw exceptions on error, so the method should always be called from inside a try/catch block.

Exemple

Exemple 54-1. Using importVendors() with a URL

  1. <?php
  2. require_once 'Net/MAC.php';  
  3. require_once 'MDB2.php';  
  4.  
  5. $db_type = 'pgsql';  
  6. $db_host = 'localhost';  
  7. $db_user = 'username';  
  8. $db_name = 'dbname';  
  9. $db_pass = 'password';  
  10.  
  11. $dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";  
  12.  
  13. $dbh =& MDB2::factory($dsn);  
  14.  
  15. if (MDB2::isError($dbh)) {  
  16. echo "MDB2 Error: ".$dbh->getUserInfo();  
  17. }  
  18.  
  19. $dboptions = array('tablename' => 'macvendors', 
  20.             'macaddrcol' => 'macaddr', 
  21.             'vendorcol' => 'vendor', 
  22.             'desccol' => 'description');  
  23.  
  24. try {  
  25. $nmh =& new Net_MAC($dbh, $dboptions);  
  26. } catch (Net_MAC_Exception $e) {  
  27. echo 'Net_MAC Error: ' . $e->getMessage();  
  28. exit;  
  29. }  
  30.  
  31. try {  
  32. $nmh->importVendors('http://anonsvn.wireshark.org/wireshark/trunk/manuf');  
  33. } catch (Net_MAC_Exception $e) {  
  34. echo 'Net_MAC Error: ' . $e->getMessage();  
  35. exit;  
  36. ?> 

This would output an error only if there is an error importing the file from the URL.

Exemple 54-2. Using importVendors() with a file, returning an array

  1. <?php
  2. require_once 'Net/MAC.php';  
  3. require_once 'MDB2.php';  
  4.  
  5. $db_type = 'pgsql';  
  6. $db_host = 'localhost';  
  7. $db_user = 'username';  
  8. $db_name = 'dbname';  
  9. $db_pass = 'password';  
  10.  
  11. $dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";  
  12.  
  13. $dbh =& MDB2::factory($dsn);  
  14.  
  15. if (MDB2::isError($dbh)) {  
  16. echo "MDB2 Error: ".$dbh->getUserInfo();  
  17. }  
  18.  
  19. $dboptions = array('tablename' => 'macvendors', 
  20.             'macaddrcol' => 'macaddr', 
  21.             'vendorcol' => 'vendor', 
  22.             'desccol' => 'description');  
  23.  
  24. $nmh =& Net_MAC::factory($dbh, $dboptions);  
  25. if (PEAR::isError($nmh)){  
  26. echo 'Net_MAC Error: '.$nmh->getMessage();  
  27. }  
  28.  
  29. $vendorArr = $nmh->importVendors('./manuf')  
  30. if (PEAR::isError(!$vendorArr)) {  
  31. echo 'Net_MAC: ' . $err->getUserInfo();  
  32. exit;  
  33. }  
  34.  
  35. print_r($vendorArr  
  36. ?> 

This would output the entire list of MAC address vendors on success and an error message on failure.

Valeurs renvoyées

throws Net_MAC_Exception


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique