获取 json 数据并传递给 PHP 字符串(此处映射 api)
get json data and pass to PHP string (Here Maps api)
我很难从 Here Maps API(涉及 JSON)获取信息。我正在尝试将 JSON 转换为字符串,以便将其保存在数据库中。
纬度和经度对我的项目最重要。
$endereco = 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil';
$endereco = str_replace(" ", "+", $endereco);
$id = 'devportal-demo-20180625';
$cod = '9v2BkviRwi9Ot26kp2IysQ';
// https://geocoder.api.here.com/6.2/geocode.json?app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&searchtext=425+W+Randolph+Chicago
//$json = file_get_contents("http://autocomplete.geocoder.api.here.com/6.2/suggest.json?app_id=$id&app_code=$cod&query=$endereco&gen=9");//
//$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?searchtext=$endereco&app_id=$id&app_code=$cod&gen=9");
$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?app_id=$id&app_code=$cod&searchtext=$endereco");
echo'<pre>';
//print_r($json = json_decode($json, true));
print_r($json = json_decode($json));
echo'</pre>';
//echo $json->{'suggestions'}[0]->{'label'};
//var_dump( $json->{'Response'}{'MetaInfo'}[0]->{'Timestamp'});
如您所见,我正在尝试使用文档页面 Here maps api 中存在的不同 json url 以及其他方法,但我想要的是一个有纬度和经度的
您正在正常使用 json_decode() 方法。它将 return 一个 OBJECT。要做你想做的事,你需要将 true 作为第二个参数传递给方法 return 结果作为 ARRAY.
此外,不要执行 str_replace() at the URI parameter, use urlencode() 方法,它将处理所有特殊字符。
您修改后的代码将是:
<?php
$endereco = "Rua Canapi 193, Guarulhos, Sao Paulo, Brasil";
$id = "here_id";
$cod = "here_cod";
$json = json_decode ( file_get_contents ( "https://geocoder.api.here.com/6.2/geocode.json?app_id=" . urlencode ( $id) . "&app_code=" . urlencode ( $cod) . "&searchtext=" . urlencode ( $endereco)), true);
echo "Longitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Longitude"] . "<br />";
echo "Latitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Latitude"] . "<br />";
echo "<pre>";
print_r ( $json);
echo "</pre>";
?>
我应用了你的样本数据here。
它创建这个呈现 url:
https://geocoder.api.here.com/6.2/geocode.json?searchtext=Rua%20Canapi%20193%2C%20Guarulhos%2C%20Sao%20Paulo%2C%20Brasil&app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&
并生成此 json 数据:
{
"Response": {
"MetaInfo": {
"Timestamp": "2018-10-17T03:35:57.353+0000"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 1,
"MatchLevel": "houseNumber",
"MatchQuality": {
"Country": 1,
"State": 1,
"City": 1,
"Street": [
1
],
"HouseNumber": 1
},
"MatchType": "pointAddress",
"Location": {
"LocationId": "NT_ySMW7zgrnTcVMzktS0t7FC_xkzM",
"LocationType": "point",
"DisplayPosition": {
"Latitude": -23.46615,
"Longitude": -46.42356
},
"NavigationPosition": [
{
"Latitude": -23.4662,
"Longitude": -46.42354
}
],
"MapView": {
"TopLeft": {
"Latitude": -23.4650258,
"Longitude": -46.4247855
},
"BottomRight": {
"Latitude": -23.4672742,
"Longitude": -46.4223345
}
},
"Address": {
"Label": "Rua Canapi, 193, Pimentas, Guarulhos - SP, 07272-060, Brasil",
"Country": "BRA",
"State": "SP",
"City": "Guarulhos",
"District": "Pimentas",
"Street": "Rua Canapi",
"HouseNumber": "193",
"PostalCode": "07272-060",
"AdditionalData": [
{
"value": "Brasil",
"key": "CountryName"
},
{
"value": "São Paulo",
"key": "StateName"
}
]
}
}
}
]
}
]
}
}
首先将json解码为对象或数组。对象语法没有任何问题,所以我将展示它。
$obj = json_decode($json);
使用对象 ->
语法隔离 Timestamp
值,不需要 curly 大括号和引号,它看起来像这样:(Demo)
echo $obj->Response->MetaInfo->Timestamp;
// 2018-10-17T03:35:57.353+0000
至于这是什么:$json->{'suggestions'}[0]->{'label'};
数据不可用。
对于 Lat 和 Long 数据,这里是充满电的电池:
代码:(Demo)
echo "Display Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Latitude;
echo "\nDisplay Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Longitude;
echo "\nNavigation Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Latitude;
echo "\nNavigation Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Longitude;
echo "\nMap View Top Left Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Latitude;
echo "\nMap View Top Left Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Longitude;
echo "\nMap View Bottom Right Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Latitude;
echo "\nMap View Bottom Right Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Longitude;
输出:
Display Position Latitude: -23.46615
Display Position Longitude: -46.42356
Navigation Position Latitude: -23.4662
Navigation Position Longitude: -46.42354
Map View Top Left Latitude: -23.4650258
Map View Top Left Longitude: -46.4247855
Map View Bottom Right Latitude: -23.4672742
Map View Bottom Right Longitude: -46.4223345
最后,要完成您的查询字符串构建任务 simpler/cleaner,请使用 http_build_query()
:
代码:(Demo)
$data = [
'searchtext' => 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil',
'app_id' => 'devportal-demo-20180625',
'app_code' => '9v2BkviRwi9Ot26kp2IysQ'
];
$json = file_get_contents('https://geocoder.api.here.com/6.2/geocode.json?' . http_build_query($data));
我很难从 Here Maps API(涉及 JSON)获取信息。我正在尝试将 JSON 转换为字符串,以便将其保存在数据库中。
纬度和经度对我的项目最重要。
$endereco = 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil';
$endereco = str_replace(" ", "+", $endereco);
$id = 'devportal-demo-20180625';
$cod = '9v2BkviRwi9Ot26kp2IysQ';
// https://geocoder.api.here.com/6.2/geocode.json?app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&searchtext=425+W+Randolph+Chicago
//$json = file_get_contents("http://autocomplete.geocoder.api.here.com/6.2/suggest.json?app_id=$id&app_code=$cod&query=$endereco&gen=9");//
//$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?searchtext=$endereco&app_id=$id&app_code=$cod&gen=9");
$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?app_id=$id&app_code=$cod&searchtext=$endereco");
echo'<pre>';
//print_r($json = json_decode($json, true));
print_r($json = json_decode($json));
echo'</pre>';
//echo $json->{'suggestions'}[0]->{'label'};
//var_dump( $json->{'Response'}{'MetaInfo'}[0]->{'Timestamp'});
如您所见,我正在尝试使用文档页面 Here maps api 中存在的不同 json url 以及其他方法,但我想要的是一个有纬度和经度的
您正在正常使用 json_decode() 方法。它将 return 一个 OBJECT。要做你想做的事,你需要将 true 作为第二个参数传递给方法 return 结果作为 ARRAY.
此外,不要执行 str_replace() at the URI parameter, use urlencode() 方法,它将处理所有特殊字符。
您修改后的代码将是:
<?php
$endereco = "Rua Canapi 193, Guarulhos, Sao Paulo, Brasil";
$id = "here_id";
$cod = "here_cod";
$json = json_decode ( file_get_contents ( "https://geocoder.api.here.com/6.2/geocode.json?app_id=" . urlencode ( $id) . "&app_code=" . urlencode ( $cod) . "&searchtext=" . urlencode ( $endereco)), true);
echo "Longitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Longitude"] . "<br />";
echo "Latitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Latitude"] . "<br />";
echo "<pre>";
print_r ( $json);
echo "</pre>";
?>
我应用了你的样本数据here。
它创建这个呈现 url:
https://geocoder.api.here.com/6.2/geocode.json?searchtext=Rua%20Canapi%20193%2C%20Guarulhos%2C%20Sao%20Paulo%2C%20Brasil&app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&
并生成此 json 数据:
{
"Response": {
"MetaInfo": {
"Timestamp": "2018-10-17T03:35:57.353+0000"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 1,
"MatchLevel": "houseNumber",
"MatchQuality": {
"Country": 1,
"State": 1,
"City": 1,
"Street": [
1
],
"HouseNumber": 1
},
"MatchType": "pointAddress",
"Location": {
"LocationId": "NT_ySMW7zgrnTcVMzktS0t7FC_xkzM",
"LocationType": "point",
"DisplayPosition": {
"Latitude": -23.46615,
"Longitude": -46.42356
},
"NavigationPosition": [
{
"Latitude": -23.4662,
"Longitude": -46.42354
}
],
"MapView": {
"TopLeft": {
"Latitude": -23.4650258,
"Longitude": -46.4247855
},
"BottomRight": {
"Latitude": -23.4672742,
"Longitude": -46.4223345
}
},
"Address": {
"Label": "Rua Canapi, 193, Pimentas, Guarulhos - SP, 07272-060, Brasil",
"Country": "BRA",
"State": "SP",
"City": "Guarulhos",
"District": "Pimentas",
"Street": "Rua Canapi",
"HouseNumber": "193",
"PostalCode": "07272-060",
"AdditionalData": [
{
"value": "Brasil",
"key": "CountryName"
},
{
"value": "São Paulo",
"key": "StateName"
}
]
}
}
}
]
}
]
}
}
首先将json解码为对象或数组。对象语法没有任何问题,所以我将展示它。
$obj = json_decode($json);
使用对象 ->
语法隔离 Timestamp
值,不需要 curly 大括号和引号,它看起来像这样:(Demo)
echo $obj->Response->MetaInfo->Timestamp;
// 2018-10-17T03:35:57.353+0000
至于这是什么:$json->{'suggestions'}[0]->{'label'};
数据不可用。
对于 Lat 和 Long 数据,这里是充满电的电池:
代码:(Demo)
echo "Display Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Latitude;
echo "\nDisplay Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Longitude;
echo "\nNavigation Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Latitude;
echo "\nNavigation Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Longitude;
echo "\nMap View Top Left Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Latitude;
echo "\nMap View Top Left Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Longitude;
echo "\nMap View Bottom Right Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Latitude;
echo "\nMap View Bottom Right Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Longitude;
输出:
Display Position Latitude: -23.46615
Display Position Longitude: -46.42356
Navigation Position Latitude: -23.4662
Navigation Position Longitude: -46.42354
Map View Top Left Latitude: -23.4650258
Map View Top Left Longitude: -46.4247855
Map View Bottom Right Latitude: -23.4672742
Map View Bottom Right Longitude: -46.4223345
最后,要完成您的查询字符串构建任务 simpler/cleaner,请使用 http_build_query()
:
代码:(Demo)
$data = [
'searchtext' => 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil',
'app_id' => 'devportal-demo-20180625',
'app_code' => '9v2BkviRwi9Ot26kp2IysQ'
];
$json = file_get_contents('https://geocoder.api.here.com/6.2/geocode.json?' . http_build_query($data));