douche3244 2018-08-16 22:26
浏览 71
已采纳

格式化API数组输出

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!

  • 写回答

1条回答 默认 最新

  • doulizhi1247 2018-08-16 22:45
    关注

    You can try several different types of tests that may work empty($v), isset($v), is_null($v), or strcmp($v, ""). Since this is a JSON it is most likely that blank values in $v are stored as a "". Therefor you should probably start with empty(). But try it and see.

    foreach ($json['Results'][0] as $k => $v){
        if (!empty($v)){
             $results .= ($k).": ".($v).'<br />';
        }
    }
    

    This will only add results if $v is not empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?