Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP code for adding new place to Google Places via API

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>";
?> 
like image 354
chriscaple Avatar asked Jun 26 '11 00:06

chriscaple


People also ask

How do I add a google places API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.

How do I get a city with Google API?

2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains address_components . Looping through the types to find locality and postal_code can give you the city name and postal code.


1 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>";
like image 56
Francois Deschenes Avatar answered Nov 15 '22 20:11

Francois Deschenes