如何在实时服务器上执行json解码


How to execute json decode in live server?

我目前在谷歌地图API工作。在谷歌地图API中,我从特定的地址获取经度和纬度。

代码如下:

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng; 

在这个纬度和经度中,值在我的活动服务器中显示为空(null)。在我的本地主机上,它工作得非常好。是否有任何东西包括显示谷歌地图?

我个人更喜欢返回数组,因为它不那么模棱两可。您可以通过在json解码上将第二个参数设置为true来实现这一点。此外,我建议假设它并不总是会返回结果,这就是为什么我添加了额外的if语句。

PHP.net函数json-decode

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');

if(strlen($geocode)>0){
  $output= json_decode($geocode, true);
  if(is_array($output) && count($output)>0)
  {
    $output[0]['location']['lat']  ;
    print_r($output); // the printr will give you a clue for the exact nesting to use above
  }
}