注意:未定义的偏移量:在第21行的C:\ xampp \ htdocs \ isan \ hasildata.php中为0

问题描述 投票:0回答:1

嗨,我的作业中收到以下错误,我不知道第21行的问题是什么问题isildata.php:

$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");

$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];

Screenshot

php arrays json maps distance
1个回答
0
投票

您必须检查您的请求是否正常

$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
if ($data['status'] !== "OK") {
    // Error
    echo $data['error_message'];

    // Do something
} else {
    $nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
    $time=$data['rows'][0]['elements'][0]['duration']['text'];
}
© www.soinside.com 2019 - 2024. All rights reserved.