Alex Alex - 5 years ago 248
PHP Question

PHP and JSON from Openstreetmap search

I am trying to use Openstreetmap and PHP to point to a place on a map.
As you can see below, I retrieve a JSON array, but PHP returns a NULL value.

Note that my

$url
is a valid JSON (you can check it here).

<?
$url = 'http://nominatim.openstreetmap.org/search/Piazza%20Duomo%20Trento?format=json&addressdetails=1&limit=1&polygon_svg=1';
$html = file_get_contents($url);
$jsonout = json_decode($html);
echo $jsonout[0];
?>


What am I doing wrong?

Answer Source

What am I doing wrong?

First of all, your openning tag is not correct :

<? ----> <?php

Then, you cannot echo an array like that. Use var_dump on your array to see the structure.

When I try your code with the corrections, I got this :

array (size=1)


0 => 
    object(stdClass)[1]
      public 'place_id' => string '8577656' (length=7)
      public 'licence' => string 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' (length=84)
      public 'osm_type' => string 'node' (length=4)
      public 'osm_id' => string '924463183' (length=9)
      public 'boundingbox' => 
        array (size=4)
          0 => string '46.0675164' (length=10)
          1 => string '46.0676164' (length=10)
          2 => string '11.1217498' (length=10)
          3 => string '11.1218498' (length=10)
      public 'lat' => string '46.0675664' (length=10)
      public 'lon' => string '11.1217998' (length=10)
      public 'display_name' => string 'Piazza Duomo, Piazza del Duomo, centro storico Trento, Trento, TN, TAA, 38122, Italia' (length=85)
      public 'class' => string 'highway' (length=7)
      public 'type' => string 'bus_stop' (length=8)
      public 'importance' => float 0.311
      public 'icon' => string 'http://nominatim.openstreetmap.org/images/mapicons/transport_bus_stop2.p.20.png' (length=79)
      public 'address' => 
        object(stdClass)[2]
          public 'bus_stop' => string 'Piazza Duomo' (length=12)
          public 'pedestrian' => string 'Piazza del Duomo' (length=16)
          public 'suburb' => string 'centro storico Trento' (length=21)
          public 'city' => string 'Trento' (length=6)
          public 'county' => string 'TN' (length=2)
          public 'state' => string 'TAA' (length=3)
          public 'postcode' => string '38122' (length=5)
          public 'country' => string 'Italia' (length=6)
          public 'country_code' => string 'it' (length=2)
      public 'svg' => string 'cx="11.1217998" cy="-46.067566399999997"' (length=40)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download