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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!