问题
Google Gears provides a geolocation API that can take LAC-CELLId information and supply lat-long data. The API is detailed here: Geolocation API I am using PHP. Here's the code that I wrote:
<?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;
?>
The response that I am getting is "JSON parsing error." What am I doing wrong?
回答1:
Don't urlencode. application/json bodies are never urlencoded.
回答2:
Are you sure you have the correct application type. Their API uses: application/json
来源:https://stackoverflow.com/questions/5715667/trouble-with-sending-json-object-over-post-to-google-gears