|
1 | | -# vin-decoder |
2 | | -### Install |
3 | | -`composer require mattsplat/vin-decode` |
| 1 | +# vin-decode |
4 | 2 |
|
5 | | -Uses National Highway Traffic Safety Administration's API to determine vehicle information |
| 3 | +[](https://github.com/mattsplat/vin-decoder/actions/workflows/ci.yml) |
| 4 | +[](https://packagist.org/packages/mattsplat/vin-decode) |
| 5 | +[](LICENSE) |
6 | 6 |
|
7 | | -#### Use |
| 7 | +A PHP wrapper for the [National Highway Traffic Safety Administration's vPIC |
| 8 | +API](https://vpic.nhtsa.dot.gov/api/) — decode VINs and look up vehicle makes, |
| 9 | +models, manufacturers, WMI codes, equipment plant codes and more. |
8 | 10 |
|
| 11 | +- One method per documented vPIC endpoint |
| 12 | +- Typed value objects for every response |
| 13 | +- Guzzle under the hood; inject your own client for testing or custom middleware |
| 14 | +- No API key required (vPIC is free and unauthenticated) |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require mattsplat/vin-decode |
| 20 | +``` |
| 21 | + |
| 22 | +Requires PHP 8.1+. |
| 23 | + |
| 24 | +## Quick start |
| 25 | + |
| 26 | +```php |
| 27 | +use Mattsplat\VinDecode\Vpic; |
| 28 | + |
| 29 | +$vpic = new Vpic(); |
| 30 | + |
| 31 | +// Decode a VIN into one flat object |
| 32 | +$vehicle = $vpic->decodeVinFlat('5UXWX7C5*BA', 2011); |
| 33 | + |
| 34 | +$vehicle->make(); // "BMW" |
| 35 | +$vehicle->model(); // "X3" |
| 36 | +$vehicle->modelYear(); // 2011 |
| 37 | +$vehicle->bodyClass(); // "Sport Utility Vehicle (SUV)/Multipurpose Vehicle (MPV)" |
| 38 | +$vehicle->engineCylinders(); // 6 |
| 39 | +$vehicle->get('PlantCity'); // "MUNICH" — any raw vPIC field |
| 40 | + |
| 41 | +// List every model a make has registered |
| 42 | +foreach ($vpic->modelsForMake('Honda') as $model) { |
| 43 | + echo $model->name, "\n"; |
| 44 | +} |
9 | 45 | ``` |
10 | | -$vin_decode = new VinDecode(); |
11 | 46 |
|
12 | | -$vin_decode->setVIN('4G2JB3249VB205377'); |
13 | | -$vin_decode->searchVIN(); |
| 47 | +List endpoints return a `Response` that is countable, iterable and array-accessible: |
| 48 | + |
| 49 | +```php |
| 50 | +$response = $vpic->allManufacturers(page: 2); |
| 51 | + |
| 52 | +$response->count(); // rows in this response |
| 53 | +$response->reportedCount; // vPIC's own Count field |
| 54 | +$response->message; // vPIC status message |
| 55 | +$response->first(); // first DTO, or null |
| 56 | +$response->all(); // list<Manufacturer> |
14 | 57 | ``` |
| 58 | + |
| 59 | +Errors (transport failure, non-2xx, non-JSON body) throw |
| 60 | +`Mattsplat\VinDecode\Exceptions\VpicRequestException`, which extends |
| 61 | +`Mattsplat\VinDecode\Exceptions\VpicException`. |
| 62 | + |
| 63 | +## Method reference |
| 64 | + |
| 65 | +<a name="method-reference"></a> |
| 66 | + |
| 67 | +### VIN decoding |
| 68 | + |
| 69 | +| Method | vPIC endpoint | Returns | |
| 70 | +| --- | --- | --- | |
| 71 | +| `decodeVin(string $vin, ?int $modelYear = null)` | `DecodeVin` | `Response<VinVariable>` | |
| 72 | +| `decodeVinFlat(string $vin, ?int $modelYear = null)` | `DecodeVinValues` | `VinResult` | |
| 73 | +| `decodeVinExtended(string $vin, ?int $modelYear = null)` | `DecodeVinExtended` | `Response<VinVariable>` | |
| 74 | +| `decodeVinFlatExtended(string $vin, ?int $modelYear = null)` | `DecodeVinValuesExtended` | `VinResult` | |
| 75 | +| `decodeVinBatch(array $vins)` | `DecodeVinValuesBatch` (POST, max 50) | `Response<VinResult>` | |
| 76 | + |
| 77 | +### WMI |
| 78 | + |
| 79 | +| Method | vPIC endpoint | Returns | |
| 80 | +| --- | --- | --- | |
| 81 | +| `decodeWmi(string $wmi)` | `DecodeWMI` | `Wmi` | |
| 82 | +| `wmisForManufacturer(string\|int $manufacturer, ?string $vehicleType = null)` | `GetWMIsForManufacturer` | `Response<WmiManufacturer>` | |
| 83 | + |
| 84 | +### Manufacturers & makes |
| 85 | + |
| 86 | +| Method | vPIC endpoint | Returns | |
| 87 | +| --- | --- | --- | |
| 88 | +| `allManufacturers(?string $manufacturerType = null, int $page = 1)` | `GetAllManufacturers` | `Response<Manufacturer>` | |
| 89 | +| `manufacturerDetails(string\|int $manufacturer, int $page = 1)` | `GetManufacturerDetails` | `Response<ManufacturerDetail>` | |
| 90 | +| `makesForManufacturer(string\|int $manufacturer)` | `GetMakeForManufacturer` | `Response<Make>` | |
| 91 | +| `makesForManufacturerAndYear(string\|int $manufacturer, int $year)` | `GetMakesForManufacturerAndYear` | `Response<Make>` | |
| 92 | +| `allMakes()` | `GetAllMakes` | `Response<Make>` | |
| 93 | + |
| 94 | +### Models |
| 95 | + |
| 96 | +| Method | vPIC endpoint | Returns | |
| 97 | +| --- | --- | --- | |
| 98 | +| `modelsForMake(string $make)` | `GetModelsForMake` | `Response<Model>` | |
| 99 | +| `modelsForMakeId(int $makeId)` | `GetModelsForMakeId` | `Response<Model>` | |
| 100 | +| `modelsForMakeYear(string $make, int $year, ?string $vehicleType = null)` | `GetModelsForMakeYear` | `Response<Model>` | |
| 101 | +| `modelsForMakeIdYear(int $makeId, int $year, ?string $vehicleType = null)` | `GetModelsForMakeIdYear` | `Response<Model>` | |
| 102 | + |
| 103 | +### Vehicle types |
| 104 | + |
| 105 | +| Method | vPIC endpoint | Returns | |
| 106 | +| --- | --- | --- | |
| 107 | +| `makesForVehicleType(string $vehicleType)` | `GetMakesForVehicleType` | `Response<Make>` | |
| 108 | +| `vehicleTypesForMake(string $make)` | `GetVehicleTypesForMake` | `Response<VehicleType>` | |
| 109 | +| `vehicleTypesForMakeId(int $makeId)` | `GetVehicleTypesForMakeId` | `Response<VehicleType>` | |
| 110 | + |
| 111 | +### Variables, equipment & parts |
| 112 | + |
| 113 | +| Method | vPIC endpoint | Returns | |
| 114 | +| --- | --- | --- | |
| 115 | +| `vehicleVariableList()` | `GetVehicleVariableList` | `Response<Variable>` | |
| 116 | +| `vehicleVariableValues(string\|int $variable)` | `GetVehicleVariableValuesList` | `Response<VariableValue>` | |
| 117 | +| `equipmentPlantCodes(int $year, int $equipmentType, string $reportType = 'All')` | `GetEquipmentPlantCodes` | `Response<PlantCode>` | |
| 118 | +| `parts(int $type, string $fromDate, string $toDate, string\|int\|null $manufacturer = null, int $page = 1)` | `GetParts` | `Response<Part>` | |
| 119 | + |
| 120 | +### Canadian vehicle specifications |
| 121 | + |
| 122 | +| Method | vPIC endpoint | Returns | |
| 123 | +| --- | --- | --- | |
| 124 | +| `canadianVehicleSpecifications(int $year, ?string $make = null, ?string $model = null, string $units = 'Metric')` | `GetCanadianVehicleSpecifications` | `Response<CanadianSpecification>` | |
| 125 | + |
| 126 | +## Documentation |
| 127 | + |
| 128 | +Per-endpoint guides with request/response examples live in [`docs/`](docs/README.md). |
| 129 | + |
| 130 | +## Custom HTTP client |
| 131 | + |
| 132 | +```php |
| 133 | +use GuzzleHttp\Client as GuzzleClient; |
| 134 | +use Mattsplat\VinDecode\Client; |
| 135 | +use Mattsplat\VinDecode\Vpic; |
| 136 | + |
| 137 | +$vpic = new Vpic(new Client(new GuzzleClient([ |
| 138 | + 'timeout' => 5, |
| 139 | + // ... proxy, middleware, retry handler, etc. |
| 140 | +]))); |
| 141 | +``` |
| 142 | + |
| 143 | +## Upgrading from 0.x |
| 144 | + |
| 145 | +The original `VinDecode\VinDecode` class still works and is kept as a thin, |
| 146 | +**deprecated** shim over `Vpic`: |
| 147 | + |
| 148 | +```php |
| 149 | +use VinDecode\VinDecode; // still works |
| 150 | + |
| 151 | +$decode = new VinDecode(); |
| 152 | +$decode->setVIN('5UXWX7C5XBL41234'); |
| 153 | +$decode->searchVIN(); // snake_cased flat array, as before |
| 154 | +echo $decode->make; |
| 155 | +``` |
| 156 | + |
| 157 | +New code should use `Mattsplat\VinDecode\Vpic` directly. See [CHANGELOG.md](CHANGELOG.md). |
| 158 | + |
| 159 | +## Contributing |
| 160 | + |
| 161 | +See [CONTRIBUTING.md](CONTRIBUTING.md). Run `composer test`, `composer lint` and |
| 162 | +`composer analyse` before opening a PR. |
| 163 | + |
| 164 | +## Security |
| 165 | + |
| 166 | +See [SECURITY.md](SECURITY.md). |
| 167 | + |
| 168 | +## License |
| 169 | + |
| 170 | +MIT — see [LICENSE](LICENSE). |
0 commit comments