2011-06-01 88 views
3

有沒有人知道在PHP,或Perl,或Python(或任何其他語言,我可以輕鬆轉換代碼)的ISO 3779汽車VIN解碼器庫作爲開源/免費軟件提供?ISO 3779車輛VIN解碼器在PHP?

即使只是解碼WMI部分(前3個位置),也會爲我節省很多時間。提前致謝。

http://en.wikipedia.org/wiki/Vehicle_identification_number

+0

看起來WMI實際上是由ISO維護的一個列表。你可以很容易地找出哪個國家(使用wiki頁面),但知道沒有這個列表的確切製造商是不可能的。這個網站已經有一個VIN查詢,但它是一個CGI:http://www.analogx.com/cgi-bin/cgivin.exe?Mode=Decode&VIN=SCCFE33C9VHF65358&Submit=Decode – James 2011-06-01 20:25:01

+0

雖然它不是PHP,但您可能會對https ://pypi.python.org/pypi/vin_decoder – 2015-03-21 15:58:25

回答

1

美國國家公路交通安全管理局具有所有WMI號碼的數據庫。這裏是一個鏈接到相關網頁:NHTSA WMI Data

他們引用ftp站點包含數據:WMI Data Source

您也可以下載565.txt文件或包含數據的MDB數據庫。這似乎是一個非常完整的數據庫。

4

我會讓你從維基百科湊數據,但下面是一個快速(模塊化),樣品可以在擴大(玩完WMI->Init()方法)。我也可能使VINLookup類成爲一個singleton或將WMI中斷到數據庫中(將數據標準化到某個地方並將其視爲像我[可能]那樣的基數爲33的數字)。

// http://en.wikipedia.org/wiki/Vehicle_identification_number#World_Manufacturer_Identifier 

define('VIN_CHARACTERS', 'ABCDEFGHJKLMNPRSTUVWXYZ1234567890'); // no I, O or Q & 0 is last. 

class WMI 
{ 
    public $country; 
    public $region; 
    public $low; 
    public $high; 

    public function __construct($country,$region,$low,$high) 
    { 
    $this->country = $country; 
    $this->region = $region; 
    $this->low = $low; 
    $this->high = $high; 
    } 

    private static function CodeToDec($code) 
    { 
    if (strlen($code) != 2) 
     return false; 

    return (strpos(VIN_CHARACTERS, $code{0}) * strlen(VIN_CHARACTERS)) + strpos(VIN_CHARACTERS, $code{1}); 
    } 

    public function IsMatch($vin) 
    { 
    // first, grab the first 2 characters 
    $code = substr($vin,0,2); 

    // next, see if it's in range 
    // we do this by converting it to a numeric 
    $_low = WMI::CodeToDec($this->low); 
    $_high = WMI::CodeToDec($this->high); 
    $_code = WMI::CodeToDec($code); 

    return (($_code >= $_low) && ($_code <= $_high)); 
    } 

    public function __toString() 
    { 
    return sprintf("%s, %s (%s, %s)", $this->country, $this->region, $this->low, $this->high); 
    } 
} 

class VINLookup 
{ 
    private $vin; 
    private $wmis = array(); 

    public function __construct($vin) 
    { 
    if (!VINLookup::IsValid($vin)) 
     throw new Exception('Invalid VIN specified'); 

    $this->vin = $vin; 

    $this->Init(); 
    } 

    private function Init() 
    { 
    $this->wmis = array(
     new WMI('South Africa',  'Africa',   'AA', 'AH'), 
     new WMI('Ivory Coast',  'Africa',   'AJ', 'AN'), 
     new WMI('(not assigned)', 'Africa',   'AP', 'A0'), 
     new WMI('Angola',   'Africa',   'BA', 'BE'), 
     new WMI('Kenya',   'Africa',   'BF', 'BK'), 

     new WMI('United States', 'North America', '1A', '10'), 
     new WMI('Canada',   'North America', '2A', '20'), 
     new WMI('Mexico',   'North America', '3A', '3W'), 
     new WMI('Costa Rica',  'North America', '3X', '37'), 
    ); 
    } 

    public function GetCountry() 
    { 
    foreach ($this->wmis as $wmi) 
    { 
     if ($wmi->IsMatch($this->vin)) 
     return $wmi; 
    } 
    return false; 
    } 

    public static function IsValid($vin) 
    { 
    return preg_match('/^[A-HJ-NPR-Z0-9]{17}$/',$vin); 
    } 
} 

用法:

// check for a valid VIN number supplied 
VINLookup::IsValid(<vin>); 


// create a new VINLookup with the specified VIN 
$lookup = new VINLookup(<vin>); 

// retrieve the _Country_ object (above), or FALSE if no country match was found. 
$lookup->GetCountry();