PHP code for adding new place to Google Places via API

风流意气都作罢 提交于 2020-01-01 04:56:07

问题


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>";
?> 

回答1:


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>";


来源:https://stackoverflow.com/questions/6481334/php-code-for-adding-new-place-to-google-places-via-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!