[PostcodeAPI] Uitlezen van JSON

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Hans Slenger

Hans Slenger

09/10/2017 14:45:54
Quote Anchor link
Hallo PHPHulp,

Ik heb een probleem met het uitlezen van JSON, ik gebruik postcodeapi en krijg de volgende JSON weer via print_r($object);
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
stdClass Object
(
    [_embedded] => stdClass Object
        (
            [addresses] => Array
                (
                    [0] => stdClass Object
                        (
                            [purpose] => winkelfunctie
                            [postcode] => 1012JS
                            [surface] => 23820
                            [municipality] => stdClass Object
                                (
                                    [id] => 0363
                                    [label] => Amsterdam
                                )

                            [city] => stdClass Object
                                (
                                    [id] => 3594
                                    [label] => Amsterdam
                                )

                            [letter] =>
                            [geo] => stdClass Object
                                (
                                    [center] => stdClass Object
                                        (
                                            [rd] => stdClass Object
                                                (
                                                    [type] => Point
                                                    [coordinates] => Array
                                                        (
                                                            [0] => 121394
                                                            [1] => 487383
                                                        )

                                                    [crs] => stdClass Object
                                                        (
                                                            [type] => name
                                                            [properties] => stdClass Object
                                                                (
                                                                    [name] => urn:ogc:def:crs:EPSG::28992
                                                                )

                                                        )

                                                )

                                            [wgs84] => stdClass Object
                                                (
                                                    [type] => Point
                                                    [coordinates] => Array
                                                        (
                                                            [0] => 4.8937176
                                                            [1] => 52.3732926
                                                        )

                                                    [crs] => stdClass Object
                                                        (
                                                            [type] => name
                                                            [properties] => stdClass Object
                                                                (
                                                                    [name] => urn:ogc:def:crs:OGC:1.3:CRS84
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [nen5825] => stdClass Object
                                (
                                    [postcode] => 1012 JS
                                    [street] => DAM
                                )

                            [addition] =>
                            [number] => 1
                            [year] => 1914
                            [province] => stdClass Object
                                (
                                    [id] => 27
                                    [label] => Noord-Holland
                                )

                            [id] => 0363200003761447
                            [type] => Verblijfsobject
                            [street] => Dam
                            [_links] => stdClass Object
                                (
                                    [self] => stdClass Object
                                        (
                                            [href] => https://api.postcodeapi.nu/v2/addresses/0363200003761447/
                                        )

                                )

                        )

                )

        )

    [_links] => stdClass Object
        (
            [self] => stdClass Object
                (
                    [href] => https://api.postcodeapi.nu/v2/addresses/?postcode=1012JS&number=1
                )

        )

)

Nu wil ik van deze uitkomst alleen de postcode in een echo hebben. Dat probeer ik als volgt:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
$postcode = $object['_embedded']['addresses']['0']['postcode'];

Als ik dit doe krijg ik een lege pagina terug. (ik zie geen foutcode.)

Dit is het PHP script wat ik gebruik:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

$curl
= curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.postcodeapi.nu/v2/addresses/?postcode=1012js&number=1",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/hal+json",
    "x-api-key: authcode staat hier!"
  ),
));


$response = curl_exec($curl);

$err = curl_error($curl);

$object = json_decode($response);
$postcode = $object['_embedded']['addresses']['0']['postcode'];

if ($err) {
  echo "cURL Error #:" . $err;
  echo "hallo?";
}
else {
echo $postcode;
}


?>

- Ariën -:
Gelieve in het vervolg bij code de [code][/code]-tags gebruiken.
Hier kan je meer lezen over de mogelijke opmaakcodes.
Alvast bedankt!
Gewijzigd op 09/10/2017 14:46:41 door - Ariën -
 
PHP hulp

PHP hulp

26/11/2024 19:28:48
 
Thomas van den Heuvel

Thomas van den Heuvel

09/10/2017 14:54:22
Quote Anchor link
Als je het wilt uitlezen als een array, zorg dan ook dat het een array is.

json_decode() heeft meerdere parameters.

Als je de tweede parameter de waarde true geeft heb je wellicht meer succes...
 
Hans Slenger

Hans Slenger

09/10/2017 17:45:41
Quote Anchor link
Argggh wat dom, excuses had dit zelf moeten opvallen..

Dank voor de reactie.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.