0
我有一小段代碼正在嘗試寫入。我試圖從json數組中提取數據,並且對我遇到的這個特定編碼有一點了解。任何方向或幫助,將不勝感激。謝謝從json中提取數據
最後一行顯示的鏈接,當在瀏覽器中加載時返回正確的路由列表沒有錯誤。
<?php
function getroute($slat,$slong,$elat,$elong)
{
$url = 'http://maps.googleapis.com/maps/api/directions/json? origin='.trim($slat).','.trim($slong).'&destination='.trim($elat).','.trim($elong).'&sensor=false&mode=driving';
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->routes->legs->distance->text;
else
return false;
}
//start
$slat = '36.019432';
$slong = '-84.227957';
//end
$elat = '36.0350416';
$elong = '-84.0199746';
$address = getroute($slat,$slong,$elat,$elong);
if($address) {echo $address;} else {echo "Not found";}
echo "
<br>
sLat: $slat<br>
sLong: $slong<br>
eLat: $elat<br>
eLong: $elong<br>";
echo 'http://maps.googleapis.com/maps/api/directions/json? origin='.trim($slat).','.trim($slong).'&destination='.trim($elat).','.trim($elong).'&sensor=false&mode=driving';
?>
我也曾嘗試:
return $data->routes[0]->legs[0]->distance->text;
和
return $data->routes[0]->legs[0]->distance[0]->text;
'$ data-> routes [0] - > legs [0] - > distance-> text;'適合我 – peaceman