我是API实施的新手。我对使用VIN解码器API显示基本车辆数据感兴趣。我创建了一个输入字段和一个提交按钮。在输入字段中输入17位VIN并选择提交后,应用程序应基于API返回车辆的年份,品牌和型号。
我一直坚持修改PHP代码以创建与html输入相关联的变量。目前,数据变量固定为3GNDA13D76S000000和5XYKT3A12CG000000,这是两个示例VIN。如何更改PHP,以便将输入输入到PHP数组中?
下面是API开发人员提供的PHP代码和一个有关如何显示数据的html框架。
<?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>
答案 0 :(得分:2)
您的代码示例很接近,但绝对缺乏研究。
响应错误,因为您忘记了指定标题Content-Type。
要提交输入,您必须指定一个表单标签,然后向按钮添加一种提交类型。
为您入门起见,我对其进行了简化,但是您需要进一步研究如何格式化和显示结果数据。目前,它只会在基本显示中回显它。
如果您想查看一些结果,可以使用19VDE1F59DE017137的示例VIN。
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-urlencoded\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'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';
}
结果:
[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] -