我在这里应用了您的示例数据。
它创建了这个呈现的 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
使用对象语法隔离值->
,不需要花括号和引号,它可以如下所示:(Demo)
echo $obj->Response->MetaInfo->Timestamp;
// 2018-10-17T03:35:57.353+0000
至于这是什么:$json->{'suggestions'}[0]->{'label'};
该数据不可用。
对于纬度和经度数据,这是完整的电池:
代码:(演示)
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
最后,为了使您的查询字符串构建任务更简单/更干净,请使用http_build_query()
:
代码:(演示)
$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));