2011-04-19 115 views
0

Google Gears提供了一個地理位置API,可以獲取LAC-CELLId信息並提供經緯度數據。 該API在此處詳述:Geolocation API 我正在使用PHP。下面是我寫的代碼:通過POST將JSON對象發送給Google Gears時遇到問題

<?php 
$urlstring="http://www.google.com/loc/json"; 
$ch=curl_init($urlstring); 

$cell_towers = array(); 
$row=new stdClass(); 
$row->location_area_code=3311; 
$row->mobile_network_code=71; 
$row->cell_id=32751; 
$row->mobile_country_code=404; 
$cell_towers[]=$row;  
$param = array(
     'host'=> 'localhost', 
     'version' => '1.1.0', 
     'request_address' => true, 
     'cell_towers' => $cell_towers 
    ); 

$param_json=json_encode($param);  
//echo $param_json."<br />"; 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch,CURLOPT_POSTFIELDS,urlencode($param_json)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/jsonrequest")); 
$result=curl_exec($ch); 
echo $result; 

>

,我得到的是響應「JSON解析錯誤。」 我在做什麼錯?

回答

3

不要urlencode。應用程序/ json的機構是從來沒有 urlencoded。

+0

該死!非常感謝,這讓我非常開心。刪除urlencode修復了它。 – citsym 2011-04-19 12:19:32

0

您確定您擁有正確的應用程序類型。他們的API使用:application/json

+0

我將該行更改爲'curl_setopt($ ch,CURLOPT_HTTPHEADER,array(「Content-Type:application/json」));'仍然存在相同的錯誤。 – citsym 2011-04-19 11:43:52

+0

我建議你看看正在生成的JSON,例如警報($ param_json)。 $ cell_towers應該是[{stuff:stuff},{stuff:stuff}]。 – Blazes 2011-04-19 11:51:40

+0

只有當我有超過1組手機信號塔的值時,[]纔會進入。如果我修改我的代碼以添加另一組值,那麼生成的JSON是:「{hostname」:「localhost」, \t「version」:「1.1.0」, \t「request_address」:true, \t 「cell_towers」: \t [ \t \t { \t \t \t 「location_area_code」:3311, \t \t \t 「mobile_network_code」:71, \t \t \t 「單元ID」:32751, \t \t \t 「mobile_country_code」:404 \t \t}, \t \t { \t \t \t 「location_area_code」:3311, \t \t \t 「mobile_network_code」:71, \t \t \t 「單元ID」:32752, \t \t \t「mobile_country_code」:404 \t \t} \t ] }' – citsym 2011-04-19 12:02:37