2011-06-26 38 views
8

我正在將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>"; 
?> 
+1

如果你表現出你寫嘗試做這個代碼這將是有益的。 – Trott

+1

這和根據給定格式進行POST請求一樣簡單。有很多不同的方法可以做到這一點,如果你想要示例代碼,如果你向我們展示了你已經使用的代碼,它會很有幫助。 (cURL?套接字?某個庫?) – deceze

回答

8

有你的代碼的幾個問題。

首先,你會使用CURLOPT_HTTPHEADERS,實際上它是CURLOPT_HTTPHEADER(沒有尾隨「S」)。該行應爲:

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

接下來,Google不希望您傳遞參數名稱,只是一個值。另一件事是$jsonpost已經是JSON,所以不需要撥打json_encode。該行應爲:

curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString); 

欲瞭解更多信息,請查看谷歌文檔:http://code.google.com/apis/maps/documentation/places/#adding_a_place

你的完整代碼,固定,測試和工作:

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

這確實完全可行。感謝澄清我出錯的地方。深表謝意。 – chriscaple

+0

這很好。 Merci,Francois。 – sehummel

+0

嗨,我沒有找到文檔添加使用API​​提供的鏈接新的地方。它是否仍然支持使用API​​添加地點? – Krunal

相關問題