Laravel 在将请求传递到函数后,我得到一个未定义的偏移量
我试图使用google distantmatrix获取到达距离所需的时间。在将所需参数传递到函数后,我得到了此错误Laravel 在将请求传递到函数后,我得到一个未定义的偏移量,laravel,google-maps-api-3,Laravel,Google Maps Api 3,我试图使用google distantmatrix获取到达距离所需的时间。在将所需参数传递到函数后,我得到了此错误 Public function GetDrivingDistance($lat1, $lat2, $long1, $long2) { $data1 = setting::where('id',1)->first(); $key = $data1->key; $url = "https://maps.googleapis.com/maps/api/dis
Public function GetDrivingDistance($lat1, $lat2, $long1, $long2) {
$data1 = setting::where('id',1)->first();
$key = $data1->key;
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL"."&key=".$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return array('distance' => $dist, 'time' => $time);
}
错误:
undefined offset: 0
在$dist=$response_a['rows'][0]['elements'][0]['distance']['text'] 尝试使用
data\u get
helper函数,如下所示:
$dist=data\u get($response\u a,'rows.0.elements.0.distance.text');
$time=data_-get($response_-a,'rows.0.elements.0.duration.text');
使用data\u get
方法,如果未找到指定的键而不是获取错误,则将返回null
有关更多信息,请参见。您能告诉我们
$response\u a
中的内容吗?什么是起点和终点坐标?可能Google只是返回NOT_FOUND,并且您在响应中没有行
元素。您是否检查了响应的状态?