我正在將Google Places API集成到一個Web應用程序中。我已經能夠成功創建代碼,以允許用戶使用API搜索地點並在應用中顯示結果,所以這很酷。通過API向Google Places添加新地點的PHP代碼
我一直無法弄清楚的是如何讓應用程序的用戶添加新的地方。 Google說的是可行的。他們這裏有一些關於它的文檔 -
https://code.google.com/apis/maps/documentation/places/#adding_a_place
但沒有示例代碼,我有一些困難,制定出PHP代碼來實現添加通話。
這是我到目前爲止的代碼。我已經用{your key}替換了我的實際API密鑰。
我不斷收到格式不正確的錯誤後 -
400這是一個錯誤。 您的客戶發佈了格式不正確或非法的請求。我們知道的就這些。
<?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>";
?>
如果你表現出你寫嘗試做這個代碼這將是有益的。 – Trott
這和根據給定格式進行POST請求一樣簡單。有很多不同的方法可以做到這一點,如果你想要示例代碼,如果你向我們展示了你已經使用的代碼,它會很有幫助。 (cURL?套接字?某個庫?) – deceze