-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvin-decoder.php
More file actions
70 lines (60 loc) · 2.32 KB
/
vin-decoder.php
File metadata and controls
70 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Retrieve vehicle details from NHTSA API using a VIN
*
* This function accepts a VIN string which is passed to the
* National Highway Traffic Safety Administration's API to be decoded
* and return details about the vehicle.
*
* Additionally, this function optionally accepts an array parameter or
* multiple string parameters to specify which details/keys to return
* from the array. If no secondary parameter is given, the entire array
* is returned.
*
* The function will always include an 'Error' key that contains 0 on
* success or an array of error messages on failure.
*
* @access public
* @link https://github.com/dynamiccookies/vin-decoder vin-decoder.php
* @link https://vpic.nhtsa.dot.gov/api/ NHTSA API
* @author Chad A Davis <github.com/dynamiccookies>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @copyright 2021 @author
* @version 0.2.0 - Please remember to check for the latest version
* @param string $vin Vehicle Identification Number
* @param mixed ...$keys (optional) Key(s) from the NHTSA array as one or
* many strings, or an array of strings
*
* @return object Vehicle
*/
function decodeVIN($vin, ...$keys) {
$message = '';
$search = '';
$url = 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/' . $vin . '?format=json';
$vehicle = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = json_decode(curl_exec($ch), true);
curl_close($ch);
if ($results) {
$message = $results['Message'];
$search = $results['SearchCriteria'];
$results = $results['Results'][0];
if (!$results['ErrorCode'] == '0') {
$vehicle['Error'] = explode(';', $results['ErrorText']);
$vehicle['Searched'] = str_replace('VIN(s): ', '', $search);
return $vehicle;
} else {$vehicle['Error'] = 0;}
$results['Make'] = ucwords(strtolower($results['Make']));
if (!$keys) {
$results['Error'] = 0;
$results['Message'] = $message;
return $results;
} else {
foreach ($keys as $key) {$vehicle[$key] = $results[$key];}
return $vehicle;
}
} else {return false;}
}
?>