28 pers. connectées au site
Manuel Pear
importVendors()
importVendors() -- Import a manufacturers' file to the database or to an array
Synopsis
- <?php
- require_once 'Net/MAC.php
- ?>
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
- <?php
- require_once 'Net/MAC.php';
- require_once 'MDB2.php';
-
- $db_type = 'pgsql';
- $db_host = 'localhost';
- $db_user = 'username';
- $db_name = 'dbname';
- $db_pass = 'password';
-
- $dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";
-
- $dbh =& MDB2::factory($dsn);
-
- if (MDB2::isError($dbh)) {
- echo "MDB2 Error: ".$dbh->getUserInfo();
- }
-
- $dboptions = array('tablename' => 'macvendors',
- 'macaddrcol' => 'macaddr',
- 'vendorcol' => 'vendor',
- 'desccol' => 'description');
-
- try {
- $nmh =& new Net_MAC($dbh, $dboptions);
- } catch (Net_MAC_Exception $e) {
- echo 'Net_MAC Error: ' . $e->getMessage();
- exit;
- }
-
- try {
- $nmh->importVendors('http://anonsvn.wireshark.org/wireshark/trunk/manuf');
- } catch (Net_MAC_Exception $e) {
- echo 'Net_MAC Error: ' . $e->getMessage();
- exit;
- ?>
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
- <?php
- require_once 'Net/MAC.php';
- require_once 'MDB2.php';
-
- $db_type = 'pgsql';
- $db_host = 'localhost';
- $db_user = 'username';
- $db_name = 'dbname';
- $db_pass = 'password';
-
- $dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";
-
- $dbh =& MDB2::factory($dsn);
-
- if (MDB2::isError($dbh)) {
- echo "MDB2 Error: ".$dbh->getUserInfo();
- }
-
- $dboptions = array('tablename' => 'macvendors',
- 'macaddrcol' => 'macaddr',
- 'vendorcol' => 'vendor',
- 'desccol' => 'description');
-
- $nmh =& Net_MAC::factory($dbh, $dboptions);
- if (PEAR::isError($nmh)){
- echo 'Net_MAC Error: '.$nmh->getMessage();
- }
-
- $vendorArr = $nmh->importVendors('./manuf')
- if (PEAR::isError(!$vendorArr)) {
- echo 'Net_MAC: ' . $err->getUserInfo();
- exit;
- }
-
- print_r($vendorArr
- ?>
This would output the entire list of MAC address vendors on success and an error message on failure.
|
Remonter