Implementing a VIN Decoder API [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Implementing a VIN Decoder API [closed]



I am new to API implementation. I am interested in using a VIN decoder API to display basic vehicle data. I have created an input field and a submit button. Once a 17 digit VIN is entered into the input field and submit is selected, the app should return the vehicle's year, make, and model based on the API.



I am stuck on modifying the PHP code to create a variable that is tied to the html input. Currently the data variables are fixed at 3GNDA13D76S000000 and 5XYKT3A12CG000000, two example VINs. How can I alter the PHP so that the input feeds into the PHP array?



Below is the PHP code provided by the API developer and a html framework for how I want the data displayed.


<?php
$postdata = http_build_query(
array(
'format' => 'json',
'data' => '3GNDA13D76S000000;5XYKT3A12CG000000'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => $postdata
)
);
$apiURL = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/";
$context = stream_context_create($opts);
$fp = fopen($apiURL, 'rb', false, $context);
if(!$fp)

echo "in first if";

$response = @stream_get_contents($fp);
if($response == false)

echo "in second if";

echo $response;?>




<!DOCTYPE html>
<html>

<head>
<title>VIN Decoder API Test</title>

<style type="text/css">
input width: 200px;
</style>

</head>

<body>

<table align="center">
<tr>
<td align="center">
<input type="text" id="b12" placeholder="Enter VIN" name="b12" maxlength="100"/>
</td>
</tr>
<tr>
<td align="center">
<button id="submit_btn">Submit</button>
</td>
</tr>
</table>
<br>
<br>

<table align="center">
<tr>
<td>Year:</td> <!--"ModelYear"-->
<td id="t2"></td>
</tr>
<tr>
<td>Make:</td> <!--"Make"-->
<td id="t3"></td>
</tr>
<tr>
<td>Model:</td> <!--"Model"-->
<td id="t4"></td>
</tr>

</table>



</body>
</html>



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Apart from mocking up a simple form you haven't made enough of an attempt for anyone to want to help you. However... In general you should submit your form to a PHP script, which will call the API and re-render the page with the VIN data.
– Jim Wright
Aug 10 at 15:57




1 Answer
1



Your code samples was close but it was definitely lacking research.



The response had an error because you forgot to specify the header Content-Type.



In order for the input to submit you had to specify a form tag and add a type of submit to the button.



I simplified it for you to look at to get you started but you would need research more how you want to format and display the resulting data afterwards. At the moment it will just echo it out in a basic display.



You can use sample VIN of 19VDE1F59DE017137 if you like to see some results.



index.html


<!DOCTYPE html>
<html>
<head>
<title>VIN Decoder API Test</title>

<style type="text/css">
input width: 200px;
</style>
</head>

<body>
<form action="api.php" method="post">
<input type="text" id="b12" placeholder="Enter VIN" name="b12" maxlength="100"/>
<button id="submit_btn" type="submit">Submit</button>
</form>
</body>
</html>



api.php


<?php

$vin = isset($_POST['b12']) ? $_POST['b12'] : '';

if ($vin)
$postdata = http_build_query([
'format' => 'json',
'data' => $vin
]
);
$opts = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencodedrn".
"Content-Length: ".strlen($postdata)."rn",
'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);

$html = '';
foreach ($json['Results'][0] as $k => $v)
$html .= '[' . $k . '] - ' . $v . '<br />';


echo $html;
else
echo 'No Vin Inputted';



Result:


[ABS] -
[AEB] -
[ActiveSafetySysNote] -
[AdaptiveCruiseControl] -
[AdaptiveDrivingBeam] -
[AdditionalErrorText] -
[AirBagLocCurtain] - All Rows
[AirBagLocFront] - 1st Row (Driver & Passenger)
[AirBagLocKnee] -
[AirBagLocSeatCushion] -
[AirBagLocSide] - 1st Row (Driver & Passenger)
[Artemis] -
[AutoReverseSystem] -
[AutomaticPedestrainAlertingSound] -
[AxleConfiguration] -
[Axles] -
[BasePrice] -
[BatteryA] -
[BatteryA_to] -
[BatteryCells] -
[BatteryInfo] -
[BatteryKWh] -
[BatteryKWh_to] -
[BatteryModules] -
[BatteryPacks] -
[BatteryType] -
[BatteryV] -
[BatteryV_to] -
[BedLengthIN] -
[BedType] -
[BlindSpotMon] -
[BodyCabType] -
[BodyClass] - Sedan/Saloon
[BrakeSystemDesc] -
[BrakeSystemType] -
[BusFloorConfigType] -
[BusLength] -
[BusType] -
[CAFEBodyType] -
[CAFEMake] -
[CAFEModel] -
[CAN_AACN] -
[CashForClunkers] -
[ChargerLevel] -
[ChargerPowerKW] -
[CoolingType] -
[Country] -
[CurbWeightLB] -
[CustomMotorcycleType] -
[DaytimeRunningLight] -
[DestinationMarket] -
[DisplacementCC] - 1999.221808
[DisplacementCI] - 122
[DisplacementL] - 1.999221808
[Doors] - 4
[DriveType] -
[DynamicBrakeSupport] -
[EDR] -
[ESC] -
[EVDriveUnit] -
[ElectrificationLevel] -
[EngineConfiguration] - In-Line
[EngineCycles] -
[EngineCylinders] - 4
[EngineHP] - 150
[EngineHP_to] -
[EngineKW] - 111.8550
[EngineManufacturer] -
[EngineModel] - R20A5
[EntertainmentSystem] -
[EquipmentType] -
[ErrorCode] - 0 - VIN decoded clean. Check Digit (9th position) is correct
[ForwardCollisionWarning] -
[FuelInjectionType] -
[FuelTypePrimary] - Gasoline
[FuelTypeSecondary] -
[GVWR] - Class 1B: 3,001 - 4,000 lb (1,360 - 1,814 kg)
[KeylessIgnition] -
[LaneDepartureWarning] -
[LaneKeepSystem] -
[LowerBeamHeadlampLightSource] -
[Make] - ACURA
[Manufacturer] - HONDA MFG., INDIANA., LLC.
[ManufacturerId] - 993
[ManufacturerType] -
[Model] - ILX
[ModelYear] - 2013
[MotorcycleChassisType] -
[MotorcycleSuspensionType] -
[NCAPBodyType] -
[NCAPMake] -
[NCAPModel] -
[NCICCode] -
[NCSABodyType] -
[NCSAMake] -
[NCSAModel] -
[Note] -
[OtherBusInfo] -
[OtherEngineInfo] -
[OtherMotorcycleInfo] -
[OtherRestraintSystemInfo] - (Rr R/L outer positions) / seat belt (Rr center position)
[OtherTrailerInfo] -
[ParkAssist] -
[PedestrianAutomaticEmergencyBraking] -
[PlantCity] -
[PlantCompanyName] -
[PlantCountry] -
[PlantState] -
[PossibleValues] -
[Pretensioner] -
[RearVisibilityCamera] -
[SAEAutomationLevel] -
[SAEAutomationLevel_to] -
[SeatBeltsAll] - Manual
[SeatRows] -
[Seats] -
[SemiautomaticHeadlampBeamSwitching] -
[Series] - 20 PREMIUM
[Series2] -
[SteeringLocation] -
[SuggestedVIN] -
[TPMS] - Direct
[TopSpeedMPH] -
[TrackWidth] -
[TractionControl] -
[TrailerBodyType] -
[TrailerLength] -
[TrailerType] -
[TransmissionSpeeds] - 5
[TransmissionStyle] - Automatic
[Trim] -
[Trim2] -
[Turbo] -
[VIN] - 19VDE1F59DE017137
[ValveTrainDesign] - Single Overhead Cam (SOHC)
[VehicleType] - PASSENGER CAR
[WheelBaseLong] -
[WheelBaseShort] -
[WheelBaseType] -
[WheelSizeFront] -
[WheelSizeRear] -
[Wheels] -
[Windows] -





Appreciate you assistance! I made additions to the code to create the exact output layout I wanted. I created variables such as: $manu = ($json['Results'][0]['Manufacturer']); that I tied to echo statements such as: echo "Manufacturer: ". $manu.'<br />';
– Phil Motto
Aug 15 at 18:19



$manu = ($json['Results'][0]['Manufacturer']);


echo "Manufacturer: ". $manu.'<br />';





Sounds good. Guess your description attracted a lot of downvotes but I figured I lead you to the right direction either way! Good luck and happy coding.
– dmotors
Aug 15 at 18:29

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS