通过API向Google地方信息添加新地点的PHP代码 [英] PHP code for adding new place to Google Places via API

查看:106
本文介绍了通过API向Google地方信息添加新地点的PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Google的Places API整合到一个网络应用程序中。我已经能够成功地创建代码,以允许用户使用API​​搜索地点并在应用中显示结果,所以这很酷。

我无法弄清楚的是如何让应用的用户添加新的地方。 Google说的是可行的。他们在这里有一些文档 -



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



但没有示例代码和我在编写PHP代码来实现添加调用时遇到一些困难。




这是我到目前为止所提供的代码。我已经用{your key}替换了我的实际API密钥。



我一直收到错误的帖子错误 -



400。这是一个错误。
您的客户发布了格式不正确或非法的请求。这就是我们所知道的。






 <?php 

ProcessCurl($ URL,$ fieldString){//启动Curl请求并返回结果
$ 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;
$
$ b $ jsonpost ='{
'location':{
lat:-33.8669710,
lng:151.1958750

精度: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>;
?>


解决方案

您的代码存在一些问题。 p>

首先,当使用 CURLOPT_HTTPHEADER CURLOPT_HTTPHEADERS (没有尾随S)。该行应为:

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

接下来,Google不希望您传递参数名称,只是一个值。另一件事是 $ jsonpost 已经是JSON,所以不需要调用 json_encode 。该行应为:

  curl_setopt($ ch,CURLOPT_POSTFIELDS,$ fieldString); 

有关更多信息,请查看Google文档: http://code.google.com/apis/maps/documentation/places/#adding_a_place



您的完整代码已修复,已测试并正常运行:

 <?php 

函数ProcessCurl($ URL,$ fieldString){//启动Curl请求并返回结果
$ 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;
$
$ b $ jsonpost ='{
'location':{
lat:-33.8669710,
lng:151.1958750

精度: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>;


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

解决方案

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

这篇关于通过API向Google地方信息添加新地点的PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆