2015-12-17 237 views
0

当我尝试通过URL调用Google Maps Geocode API时,它工作正常并返回正确的JSON。Google Map Geocode API返回null

例如,这是网址:

https://maps.googleapis.com/maps/api/geocode/json?address=2140+Amphitheatre+Parkway,+Mountain+View,+IN&key=AIzaSyCUDSJ2GBE1DHupbAZT4u8gZqclkIhmb0M

当我试图用我的PHP代码相同,则返回null。

下面是我的代码:

<?php 
  
function lookup($string){ 
  
   $string = str_replace (" ", "+", urlencode($string)); 
   $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false"; 
  
   $ch = curl_init(); 
   curl_setopt($ch, CURLOPT_URL, $details_url); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   $response = json_decode(curl_exec($ch), true); 
  
   // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST 
   if ($response['status'] != 'OK') { 
    return null; 
   } 
  
   print_r($response); 
   $geometry = $response['results'][0]['geometry']; 
  
    $longitude = $geometry['location']['lat']; 
    $latitude = $geometry['location']['lng']; 
  
    $array = array(
        'latitude' => $geometry['location']['lng'], 
        'longitude' => $geometry['location']['lat'], 
        'location_type' => $geometry['location_type'], 
    ); 
  
    return $array; 
  
} 
  
$city = 'San Francisco, USA'; 
  
$array = lookup($city); 
var_dump($array); 
  
?> 
</pre> 

我尝试添加和删除API密钥,但它仍然返回null。

欣赏任何支持。

+0

你是否包含了你需要的一切? +打开错误报告?也许你在滚动一个错误或什么的? – Naruto

+0

它的工作正常,我检查了我的本地主机和工作。 我正在此 阵列(3){ [ “纬度”] => 浮子(-122.4194155) [ “经度”] => 浮子(37.7749295) [ “LOCATION_TYPE”] => 串(11)“APPROXIMATE” – Vivek

+0

我很困惑,为什么它不返回任何东西 – dev1234

回答

0

看起来代码是好的,它运行良好,你只需要检查curl是否启用。我认为这是你无法使用php获取数据的原因。

相关问题