I am new to API integration and PHP. I recently integrated a VIN decoder API into my app. If a vehicle's VIN is entered into the input bar and <kbd>submit</kbd> is selected, the API will retrieve relevant data concerning that vehicle. For example, if I enter WAUBFAFL6FA058452
and click <kbd>submit</kbd>, I will receive data that this VIN belongs to a 2015 Audi A4 Sedan.
The issue I am running into is that not all data fields return with data. For instance, WAUBFAFL6FA058452
returns:
Make: Audi
Model: A4
ModelYear: 2015
VehicleType: PASSENGER CAR
Series: Premium quattro
However, about twenty or so other fields are returned blank such as:
DriveType:
DynamicBrakeSupport:
EDR:
ESC:
EVDriveUnit:
ElectrificationLevel:
How can I create an if
statement within my PHP that only returns categories that contain data? In other words, categories such as Make
would be visible to the user but categories such as DriveType
won't be. Visibility of categories is dependent on that category having data. This needs to be structured as an if
statement because some VINs carry more data than others and vice versa.
I have tried for about an hour now, and have been unsuccessful.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>VIN Decoder API Test</title>
<style type="text/css">
input,button {width: 200px;display: block;margin-left: auto;margin-right: auto;}
button {width: 100px;background-color: darkgray;}
</style>
</head>
<body>
<form action="processvin.php" method="post">
<input type="text" id="b12" placeholder="Enter VIN" name="b12" maxlength="100"/>
<br>
<button id="submit_btn">Submit</button>
</form>
<br>
<br>
</body>
</html>
processvin.php
<?php
$vin = $_POST["b12"];
if ($vin) {
$postdata = http_build_query([
'format' => 'json',
'data' => $vin
]
);
$opts = [
'http' => [
'method' => 'POST',
'content' => $postdata
]
];
$apiURL = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/";
$context = stream_context_create($opts);
$fp = fopen($apiURL, 'rb', false, $context);
$line_of_text = fgets($fp);
$json = json_decode($line_of_text, true);
fclose($fp);
foreach ($json['Results'][0] as $k => $v){
$results .= ($k).": ".($v).'<br />';
}
echo $results;
}
else {
echo 'No Vin Inputted';
}
?>
Any help, suggestions, pointers, etc will not be taken for granted! Thanks so much!