如何将PHP中发现的特定数据返回给JSON调用


How to return no particular data found in PHP to JSON call

由于用户从API输入的结果不同,我需要打印特定元素没有结果的消息,并打印其他有结果的消息。例如,我想说这个变量没有结果:$model = $jfo ->发动机->气门->正时;下面是我的代码,这里是到API的链接:http://api.edmunds.com/api/vehicle/v2/vins/1GCGG25U261243927?fmt=json& api_key = 2 jwwaax2sc4kuq7vg7ud79rc

<?php
//ini_set("display_errors",1);
//exit;
$datvin="";
$title="";
$posts="";
$mds="";
$model="";
if (isset($_POST['vin'])){
$datvin=trim($_POST['vin']);
$json_file = file_get_contents('http://api.edmunds.com/api/vehicle/v2/vins/'.$datvin.'?fmt=json&api_key=2jwwaax2sc4kuq7vg7ud79rc');
// convert the string to a json object
$jfo = json_decode($json_file);
if ($jfo != '' && $jfo !== null) {
// read the title value
$title = $jfo->make->name;
// copy the posts array to a php var
$posts = $jfo->make->id;
$mds = $jfo->years[0]->year;
$model = $jfo ->engine->valve->timing;

}else {
    $model="not found";
}

}

?>
 <form role="form" action="index.php" method="post">
          <div class="form-group">
              <label for="VIN" class="hidden-lg hidden-md">Enter your VIN</label>
              <input type="text" class="form-control" id="VIN" name="vin" placeholder="Enter your VIN" required="required">
          </div>
          <button type="submit" class="btn btn-primary btn-centered"><i class="fa fa-search"></i> Test VIN</button>
        </form>
        <div><?php echo $model; ?></div>
<?php
//ini_set("display_errors",1);
//exit;
$datvin="";
$title="";
$posts="";
$mds="";
$model="";
if (isset($_POST['vin'])){
$datvin=trim($_POST['vin']);
@$str = file_get_contents('http://api.edmunds.com/api/vehicle/v2/vins/'.$datvin.'?fmt=json&api_key=2jwwaax2sc4kuq7vg7ud79rc');
// convert the string to a json object
$json = json_decode($str, true);
//echo count($json);
 //key to search
 $json['vin'];
 //show results
if($_POST['vin'] ==$json['vin']){
        echo "Model is here!";
        echo $json['make']['id'];
        echo $json['make']['name'];
        echo $json['make']['niceName'];
        echo $json['categories']['market'];
        echo $json['model']['id'];
        echo $json['model']['name'];
        echo $json['model']['niceName'];
    }
    else {
        $model="Model not found";
    }
}