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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!