Abaij
Abaij

Reputation: 873

How to make a request of Google Geolocation API?

I was thinking that using google geolocation request is the same as using google distance matrix, but I'm wrong.

So, I don't understand how it works. I tried putting this url in the address bar

https://www.googleapis.com/geolocation/v1/geolocate?key=MyAPIKey

I got nothing but Not found response. I'm expecting a response like:

{  
   "location": {
      "lat": 51.0,
      "lng": -0.1
   },
   "accuracy": 1200.4
}

Then I read there are some request body information in json that also have to be sent in the request but I don't know how to include them into the request. I want to do it with php. Please any help would be appreciated. Thanks.

Upvotes: 1

Views: 842

Answers (2)

Franco Vizcarra
Franco Vizcarra

Reputation: 426

You can do it by use native php http stream or with curl, here's an article explaining both methods POST data to a URL in PHP .

In your case there's no need for the $data or curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars); lines as you don't need to send any parameters.

Upvotes: 1

SimonAntony
SimonAntony

Reputation: 84

It's because you are calling it as a GET request, the api specifically states it must be made as a POST request. You will then get the JSON you expect to see.

Upvotes: 1

Related Questions