使用googlemaps的php服务器端地理编码返回状态确定但没有别的

时间:2016-11-25 11:14:43

标签: php google-maps curl

我正在尝试使用php在服务器端进行地理编码。以下是我的代码:

static private function curl_file_get_contents($URL){
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    curl_close($c);

    if ($contents) return $contents;
        else return FALSE;
}


public function preview(Request $request)
{
    $url = "https://maps.googleapis.com/maps/api/geocode/json?key=my_api_key&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";

    $resp_json = self::curl_file_get_contents($url);
    $resp = json_decode($resp_json, true);

    if($resp['status']='OK'){
        dd($resp);
    }else{
        dd('not ok');
    }

当我运行时,我的状态是正常的,并且dd($ resp);得到执行,它只输出:

array:1 [▼
  "status" => "OK"
]

我在这里查看了json响应示例:https://developers.google.com/maps/documentation/geocoding/intro

并且应该返回更多但是在我的情况下它不会被返回。我不知道为什么,这可能与我在本地运行这个有关,虽然在这种情况下它不应该返回状态:OK

我已经在这里举例http://erlycoder.com/45/php-server-side-geocoding-with-google-maps-api-v3

编辑: 如果我直接将其传入浏览器

  

https://maps.googleapis.com/maps/api/geocode/json?key=my_api_key&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

然后它返回更多数据

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "USA",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4223664,
               "lng" : -122.084406
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4237153802915,
                  "lng" : -122.0830570197085
               },
               "southwest" : {
                  "lat" : 37.42101741970851,
                  "lng" : -122.0857549802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

2 个答案:

答案 0 :(得分:0)

我已经通过添加

解决了这个问题
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);

答案 1 :(得分:-1)

请检查您的服务器配置并检查是否支持curl。 使用此代码检查您的服务器配置

public static void MethodName{bool first, bool second, bool third}
{
   //Do something
}