2011-04-19 34 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