【问题标题】:Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\isan\hasildata.php 中的 0
【发布时间】:2019-02-22 04:14:47
【问题描述】:

您好,我在作业中收到以下错误,我不知道第 21 行的 hasildata.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

【问题讨论】:

  • 您可以尝试var_dump($dataJson) 并将内容包含在您的问题中。

标签: php arrays json maps distance


【解决方案1】:

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

$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'];
}

【讨论】:

  • 当我将密钥更改为谷歌程序时,它运行顺利,没有错误,感谢您的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多