So I'm trying to parse some JSON that the Youtube API returns. I've been breaking my head over understanding how to parse JSON in PHP and I can't seem to figure it out.
This is basically the output of the API:
{
"kind": "youtube#channelListResponse",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/mgiCAEvrnAFhKSCga80wVfAbUtc\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/cndol6pHIKmkRCizokwycOOUr2E\"",
"id": "SWMb9NxQL9I6c",
"snippet": {
"title": "Koffee With Karan",
"description": "Catch Karan Johar chat up Bollywood, secrets revealed and stories told , that's how the world will remember Koffee with Karan",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/sh/554333074/showposter_thumb.jpg"
},
"medium": {
"url": "https://i.ytimg.com/sh/554333074/showposter.jpg"
},
"high": {
"url": "https://i.ytimg.com/sh/554333074/showposter_hq.jpg"
}
}
}
}
]
}
What I want to get is pretty much everything in "snippet", I need the title, description and thumbnails. I've been trying to access it like this:
$getjson = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=SWMb9NxQL9I6c&key=mykey');
$data = json_decode($getjson,true);
echo $data['items'][0]['snippet'][0]['title'];
or even trying something like
echo $data['items']->snippet->title;
I've found numerous articles explaining how but nothing seems to work or make sense. First of all, the "items" data is outside of the starting { } brackets, which I haven't really been able to find anything about.
Any help and primarily an explanation about how this works would be awesome. I just want to understand the syntax needed to access this stuff.