【发布时间】:2019-01-24 17:20:40
【问题描述】:
我是 API 集成和 PHP 的新手。我最近在我的应用程序中集成了一个 VIN 解码器。在输入框中输入车辆的 VIN,选择提交,API 数据库中有关该车辆的所有信息都会显示出来。
数据存储为关联数组,包含类别及其对应元素。例如对于 VIN:WAUBFAFL6FA058452 类别之一是制造,其元素是奥迪。
一些 VIN 比其他 VIN 携带更多的数据。我只希望在选择提交时显示带有数据的类别。所以在一些帮助下,我添加了这行代码:
foreach ($json['Results'][0] as $k => $v){
if (!empty($v)) {
$results .= ($k).": ".($v).'<br />';
}
}
我的下一个障碍是获取输出、类别及其元素,并组织/格式化它们,同时保持我上面定义的逻辑。我只想显示带有数据的类别。
我不确定如何定位单个类别来格式化它们。目前,输出按字母顺序显示。我想定义自己的输出布局。
电流输出:
AirBagLocCurtain: All Rows
AirBagLocFront: 1st Row (Driver & Passenger)
AirBagLocSide: 1st Row (Driver & Passenger)
BodyClass: Sedan/Saloon
DisplacementCC: 1984
DisplacementCI: 121.071108283
DisplacementL: 1.984000
And so on....
如何更改此布局?我希望将输出布局更改为以下内容。
期望的输出:
VIN: WAUBFAFL6FA058452
General –
Make: AUDI
ModelYear: 2015
Model: A4
BodyClass: Sedan/Saloon
Doors: 4
Series: Premium quattro
VehicleType: PASSENGER CAR
Safety -
AirBagLocCurtain: All Rows
AirBagLocFront: 1st Row (Driver & Passenger)
AirBagLocSide: 1st Row (Driver & Passenger)
SeatBeltsAll: Manual
Engine -
DisplacementCC: 1984
DisplacementCI: 121.071108283
DisplacementL: 1.984000
EngineCylinders: 4
EngineHP: 220
EngineKW: 164.0540
EngineManufacturer: Audi
EngineModel: Flex Fuel Capable engine
TransmissionStyle: Automatic
OtherEngineInfo: Fuel: Gas (50-St); Federal / California Emission Standard: BIN 5 / ULEV II; Emissions Certification Test Group: FVGAV02.0AUB / FVGAJ02.0AUF E85
FuelTypePrimary: Gasoline
FuelTypeSecondary: Ethanol (E85)
Factory -
Manufacturer: AUDI
ManufacturerId: 1149
PlantCity: Ingolstadt
PlantCountry: Germany
Other -
ErrorCode: 0 - VIN decoded clean. Check Digit (9th position) is correct
TPMS: Indirect
这是我的html代码,只是输入栏和提交按钮:
<!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="processvin3.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>
还有我的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){
if (!empty($v)) {
$results .= ($k).": ".($v).'<br />';
}
}
echo $results;
}
else {
echo 'No Vin Inputted';
}
?>
非常感谢任何建议、提示或答案。非常感谢。
【问题讨论】:
-
我们得到 404 错误