0

I'm trying to add a place using the google places API.

The API IS enabled and the API key is valid. Here is my code:

$url = "https://maps.googleapis.com/maps/api/place/add/json?key=MYKEY";
$content = json_encode('{
"location": {
"lat": 55.9868532,
"lng": -4.5780577
},
"accuracy": 50,
"name": "Home Made Pizza",
 "types": ["other"],
"website": "http://example.com",
"language": "en-AU"
}');

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
    array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
die("response $json_response");
};


curl_close($curl);

$response = json_decode($json_response);

The response I am getting is

response { "status" : "REQUEST_DENIED" }

Help :-)

Lyrical.me
  • 215
  • 3
  • 15

2 Answers2

0

Based from this thread, make sure that you have enabled your API in the Console.

You are using http(s) for calling Google API, is your html also hosted under http(s). If not try changing google url to http.

Otherwise every thing else kinda looks ok so that makes me think you might wanna check your api console again, go to SERVICES and check if your 'Places API' is turned ON.

You can also try changing the Port address to 443 to get response from Places API

Here are some links which might also help:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
0

The problem here was that I was using "json_encode" on json.

I did not need to encode it and this was throwing an error.

Morale of the story, don't Json_encode Json.

Lyrical.me
  • 215
  • 3
  • 15