获取json数据并传递给PHP字符串(Here Maps api)
I am having difficulty getting information from the Here Maps API (which deals with JSON). I am trying to convert the JSON to a string so it can be saved in a database.
Latitude and longitude is most important for my project.
$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'});
As you can see I'm trying with the different json url that exists in the documentation page Here maps api and there are other ways but what I want is the one that has latitude and longitude
You're using json_decode() method as normal way. It'll return an OBJECT. To do what you want, you need to pass true as second parameter to method return the result as an ARRAY.
Also, don't do str_replace() at the URI parameter, use urlencode() method, that will handle all special characters.
Your revised code will be:
<?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>";
?>
I applied your sample data here.
It creates this rendered 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&
and generated this json data:
{
"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"
}
]
}
}
}
]
}
]
}
}
First decode the json into an object or array. There is nothing wrong with object syntax, so I'll show that.
$obj = json_decode($json);
Isolating the Timestamp
value using object ->
syntax, doesn't require curly braces and quoting, it can look like this: (Demo)
echo $obj->Response->MetaInfo->Timestamp;
// 2018-10-17T03:35:57.353+0000
As for whatever this is: $json->{'suggestions'}[0]->{'label'};
that data isn't available.
For the Lat and Long data, here's the full battery:
Code: (Demo)
echo "Display Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Latitude;
echo "
Display Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Longitude;
echo "
Navigation Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Latitude;
echo "
Navigation Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Longitude;
echo "
Map View Top Left Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Latitude;
echo "
Map View Top Left Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Longitude;
echo "
Map View Bottom Right Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Latitude;
echo "
Map View Bottom Right Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Longitude;
Output:
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
Finally, to make your querystring building task simpler/cleaner, use http_build_query()
:
Code: (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));