Trouble with Sending JSON object over POST to Google Gears

北慕城南 提交于 2019-12-25 02:26:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!