简体   繁体   中英

Can't see JSON data - PHP

I was trying to read simple content from http://ergast.com/api/f1/drivers/kobayashi.json with a PHP file, but I can't see anything.

<?php

$file = file_get_contents('http://ergast.com/api/f1/drivers/vettel.json/');
$data = json_decode($file,true);

echo $data['MRData']['DriverTable']['Drivers']['familyName'];

?>

Edit 1:

<?php

$file = file_get_contents('http://ergast.com/api/f1/drivers/vettel.json');
$data = json_decode($file,true);

echo $file;
echo $data;
echo $data['MRData']['DriverTable']['Drivers']['0']['familyName'];

?>

File is the same, but seems I can't reach file because I can't see anything if I open the file. :(

Remove the slash and after the driver result is coming in array.

u can check that with print_r debugging option.

$file = file_get_contents('http://ergast.com/api/f1/drivers/vettel.json');
$data = json_decode($file,true);
//echo "<pre>";
//print_r($data);
//echo "</pre>";
echo $data['MRData']['DriverTable']['Drivers'][0]['familyName'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM