I know this is probably something simple and I am beginning to understand how I need to go about this, I realize that this has something to do with OO however as i am new to OO and json im having a bit of a problem with this http://api.discogs.com/database/search?q=d&page=1&per_page=5 it returns 5 results with the query d
in json format so i parse it with the php function and then attempt to lets say show the thubmnail for the first results this is my parse code:
$returnData = file_get_contents($queryURL);
$discogsJSON = json_decode($returnData);
where $queryURL
= the provided url. if i print $discogsJSON
i get this:
<pre>stdClass Object
(
[pagination] => stdClass Object
(
[per_page] => 5
[items] => 179465
[page] => 1
[urls] => stdClass Object
(
[last] => http://api.discogs.com/database/search?q=f&per_page=5&page=35893
[next] => http://api.discogs.com/database/search?q=f&per_page=5&page=2
)
[pages] => 35893
)
[results] => Array
(
[0] => stdClass Object
(
[style] => Array
(
[0] => Free Jazz
)
[thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
[format] => Array
(
[0] => Vinyl
[1] => LP
[2] => Album
)
[country] => Switzerland
[title] => Steve Lacy - Clinkers
[uri] => /Steve-Lacy-Clinkers/master/54331
[label] => Array
(
[0] => Hat Hut Records
)
[catno] => F
[year] => 1978
[genre] => Array
(
[0] => Jazz
)
[resource_url] => http://api.discogs.com/masters/54331
[type] => master
[id] => 54331
)
[1] => stdClass Object
(
[style] => Array
(
[0] => Free Jazz
)
[thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
[format] => Array
(
[0] => Vinyl
[1] => LP
[2] => Album
)
[country] => Switzerland
[title] => Steve Lacy - Clinkers
[uri] => /Steve-Lacy-Clinkers/release/1672955
[label] => Array
(
[0] => Hat Hut Records
)
[catno] => F
[year] => 1978
[genre] => Array
(
[0] => Jazz
)
[resource_url] => http://api.discogs.com/releases/1672955
[type] => release
[id] => 1672955
)
[2] => stdClass Object
(
[style] => Array
(
[0] => Alternative Rock
[1] => Goth Rock
[2] => Heavy Metal
)
[thumb] => http://api.discogs.com/image/R-90-2785283-1300918365.jpeg
[format] => Array
(
[0] => CD
[1] => Compilation
[2] => Unofficial Release
)
[country] => Russia
[title] => HIM (2) - Overload Hits
[uri] => /HIM-Overload-Hits/release/2785283
[label] => Array
(
[0] => FullHouse Records
)
[catno] => F?????
[year] => 2003
[genre] => Array
(
[0] => Rock
)
[resource_url] => http://api.discogs.com/releases/2785283
[type] => release
[id] => 2785283
)
[3] => stdClass Object
(
[style] => Array
(
[0] => Drone
[1] => Ambient
)
[thumb] => http://api.discogs.com/image/R-90-3108621-1316178067.jpeg
[format] => Array
(
[0] => CDr
[1] => Album
[2] => Limited Edition
)
[country] => Russia
[title] => Solar Ocean - Twillight October
[uri] => /Solar-Ocean-Twillight-October/release/3108621
[label] => Array
(
[0] => Necrophone
)
[catno] => F
[year] => 2010
[genre] => Array
(
[0] => Electronic
)
[resource_url] => http://api.discogs.com/releases/3108621
[type] => release
[id] => 3108621
)
[4] => stdClass Object
(
[style] => Array
(
[0] => Techno
[1] => Electro
[2] => Tech House
[3] => Minimal
)
[thumb] => http://api.discogs.com/image/R-90-699054-1161976299.jpeg
[format] => Array
(
[0] => Vinyl
[1] => 12"
[2] => Box Set
[3] => Compilation
[4] => Limited Edition
)
[country] => Germany
[title] => Various - Cocoon Compilation F
[uri] => /Various-Cocoon-Compilation-F/master/33655
[label] => Array
(
[0] => Cocoon Recordings
[1] => Cocoon Compilation
)
[catno] => COR LP 011
[year] => 2006
[genre] => Array
(
[0] => Electronic
)
[resource_url] => http://api.discogs.com/masters/33655
[type] => master
[id] => 33655
)
)
)
</pre>
which is exactly correct and i can see all the data i want to, however when i try to access the (lets say thumbnail of the first) like so:
echo $discogsJSON['results']['0']['thumb'];
i am left with: Fatal error: Cannot use object of type stdClass as array
I suppose from here there is enough information to go off of to construct the correct echo statement, but can anyone explain to me the OO theory behind the correct way to get the data out?
$discogsJSON
is not an array, but an stdClass
object. results is a property on this object.
This should do what you are looking for.
echo $discogsJSON->results['0']->thumb;
However, it may be more convinient to add a true
flag to json_decode
to make sure that the returned value is an associative array.
$discogsJSON = json_decode($returnData, true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With