Home » Php » PHP code for adding new place to Google Places via API

PHP code for adding new place to Google Places via API

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m working on integrating Google’s Places API into a web app. I’ve been able to successfully create code to allow users to search for places using the API and to display results in the app, so that’s cool.

What I haven’t been able to figure out is how to allow users of the app to add new places. Something which Google says is doable. They have some documentation on it here –

https://code.google.com/apis/maps/documentation/places/#adding_a_place

But no example code and I’m having some difficulty working out PHP code to implement the add call.


This is the code that I’ve come up with so far. I’ve replaced my actual API key with {your key} here.

I keep getting a malformed post error –

400. That’s an error.
Your client has issued a malformed or illegal request. That’s all we know.


<?php

function ProcessCurl($URL, $fieldString){ //Initiate Curl request and send back the result
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($fieldString)));
        $resulta = curl_exec ($ch);
        if (curl_errno($ch)) {
                print curl_error($ch);
        } else {
        curl_close($ch);
        }
        echo $resulta;
    }

$jsonpost = '{
  "location": {
    "lat": -33.8669710,
    "lng": 151.1958750
   },
  "accuracy": 50,
   "name": "Daves Test!",
   "types": ["shoe_store"],
  "language": "en-AU"
}';
 $url = "https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key={your key}";

 $results = ProcessCurl ($url, $jsonpost);

 echo $results."<BR>";
?> 
How to&Answers:

There are a few problems with your code.

First, you’d usinre CURLOPT_HTTPHEADERS when in fact, it’s CURLOPT_HTTPHEADER (without the trailing “S”). That line should read:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

Next, Google doesn’t want you to pass a parameter name, just a value. The other thing is that the $jsonpost is already JSON so there’s no need to call json_encode. The line should read:

curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);

For more information, check the Google documentation: http://code.google.com/apis/maps/documentation/places/#adding_a_place.

Your complete code, fixed, tested and working:

<?php

function ProcessCurl($URL, $fieldString){ //Initiate Curl request and send back the result
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
        $resulta = curl_exec ($ch);
        if (curl_errno($ch)) {
                print curl_error($ch);
        } else {
        curl_close($ch);
        }
        echo $resulta;
    }

$jsonpost = '{
  "location": {
    "lat": -33.8669710,
    "lng": 151.1958750
   },
  "accuracy": 50,
   "name": "Daves Test!",
   "types": ["shoe_store"],
  "language": "en-AU"
}';

$url = "https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key=";

$results = ProcessCurl ($url, $jsonpost);

echo $results."<BR>";