GeolocationAPI 使用

很多朋友在使用google GeolocationAPI 接口測試基站定位,測試時需要往接口http://www.google.com/loc/json提交json格式的數據,json格式參數比較多,在IDE裏測試起來也比較麻煩,有時因爲一個語法錯誤不得不排查很長時間。

這裏ant推薦一個比較簡單的方法來測試json數據格式是否正確:使用curl測試。

curl是一個利用URL語法在命令行方式下工作的文件傳輸工具。使用curl來提交http GET/POST數據很是方便。curl是Unix/Linux下常用的工具,也有windows版本。

GeolocationAPI 的詳細語法這裏就不介紹了。

例如我們想通過http://www.google.com/loc/json查詢LAC:14556 ,CELLID:36525 的基站位置。根據GeolocationAPI 裏提到的語法,提交的數據應該是這樣的格式:
{
"version": "1.1.0" ,
"host": "maps.google.com",
"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
"home_mobile_country_code": 460,
"home_mobile_network_code":0,
"address_language": "zh_CN",
"radio_type": "gsm",
"request_address": true ,
"cell_towers":[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}

下面我們通過curl測試該格式是否正確。

命令格式: curl -d ‘post的數據’ http://www.google.com/loc/json

這裏只需要用到curl的-d參數,-d後面跟的是post的數據內容

在命令行下輸入” curl -d ‘ “後回車,粘貼上面的json格式數據回車,再輸入” ‘ http://www.google.com/loc/json”回車,就可以看到google的返回結果了。

通過curl可以方便的測試各種json參數組合。













更具以上接口說明,我寫了以下可用代碼

<?php
function curl_post($url, $vars, $second=30)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$vars = '{
"version": "1.1.0" ,
"host": "maps.google.com",
"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
"home_mobile_country_code": 460,
"home_mobile_network_code":0,
"address_language": "zh_CN",
"radio_type": "gsm",
"request_address": true ,
"cell_towers":[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}';
$rdata = curl_post('http://www.google.com/loc/json',$vars);
$r_ary = (array) json_decode($rdata);
print_r($r_ary['location']);
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章