douge7771 2018-10-17 03:13
浏览 69
已采纳

获取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

  • 写回答

2条回答 默认 最新

  • douan2907 2018-10-17 03:21
    关注

    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>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序