-1

I'm trying to decode the youtube api. I don't need everything, just a few pieces, but I'm having trouble with the "items" array.

youtube api snippet

My code is very very simple.

$url = file_get_contents("THIS IS MY YOUTUBE API ENDPOINT");
$json = json_decode($url, true);

If I echo out

echo $json['kind']."<p>".$json['etag']."<p>".$json['nextPageToken']."<p>";
echo $json['pageInfo']['totalResults']."<p>".$json['pageInfo']['resultsPerPage'];

Then I would see:

  • youtube#activityListResponse
  • that super long etag that I don't feel like typing
  • CAEQAA
  • 10
  • 1

But I want the information within ITEMS

If I try print_r($json['items'][0]); Then I get ALL of the information from items as an array, spit out like this

Array ( [kind] => youtube#activity [etag] => "ZG3FIn5B5vcHjQiQ9nDOCWdxwWo/9bAEAi43B5b3tiYpW1BAPw2aZ54" [id] => VTE1MjQwOTM0MjI5NDIyNjY4MzQ2NjUxMg== [snippet] => Array ( [publishedAt] => 2018-04-18T23:17:02.000Z [channelId] => UC6TEaGms62zd11sdt_z1UAg [title] => AppyBuilder: Create a High Score Leaderboard with Wheel of Fortune [description] => TUTORIAL DIFFICULTY: Medium/Hard You should be able to follow along easily if you are able to find blocks by looking at a screenshot. This tutorial uses Fusion Table as the Database to store/retrieve user data. You are required to already have a basic knowledge of how Fusion Table works in order to complete this tutorial. VIDEO DESCRIPTION: This video builds off the Fusion Table tutorials. Giving you additional examples on how to easily check for a Username/Password in the Fusion Table (Screen1), how to update the user's Score with a Spinning Wheel Event (EVENT Screen) and how to view all user's high scores (LEADERBOARD Screen). The tutorial focuses on the Leaderboard Screen which shows you how to grab the necessary information from the Fusion Table and output that data using a Custom List View. The Custom List will show the user's avatar, username and high score in order from highest to lowest. VIDEO CHAPTERS: Introduction - 00:00 Getting Started - 00:13 Design View - 01:19 Blocks Editor - 03:32 Testing the App - 07:34 Wrap Up - 08:00 Pixii Bomb Squad - 09:48 AppyBuilder Community - 09:53 Goodbye - 10:00 DOWNLOAD PROJECT .aia FILE: http://community.appybuilder.com/t/high-score-leaderboard-using-fusion-table-as-the-database/8528 FACEBOOK PAGE: https://www.facebook.com/pixiibomb PATREON PAGE: https://www.patreon.com/pixiibomb AppyBuilder: http://appybuilder.com/ AppyBuilder Community: http://community.appybuilder.com/ FREE RESOURCES Although I do create a lot of my images, to save time in Tutorials or quick projects, I like to use a free resource sites. My favorites are: http://www.freepik.com/ http://www.flaticon.com/ [thumbnails] => Array ( [default] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/default.jpg [width] => 120 [height] => 90 ) [medium] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/mqdefault.jpg [width] => 320 [height] => 180 ) [high] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/hqdefault.jpg [width] => 480 [height] => 360 ) [standard] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/sddefault.jpg [width] => 640 [height] => 480 ) [maxres] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/maxresdefault.jpg [width] => 1280 [height] => 720 ) ) [channelTitle] => Pixii Bomb [type] => upload ) [contentDetails] => Array ( [upload] => Array ( [videoId] => xOCI9viNun8 ) ) ) youtube#activity"ZG3FIn5B5vcHjQiQ9nDOCWdxwWo/9bAEAi43B5b3tiYpW1BAPw2aZ54"VTE1MjQwOTM0MjI5NDIyNjY4MzQ2NjUxMg==

If I try foreach($json['items'][0] as $key=>$value){ echo $value; }

Then I get: Notice: Array to string conversion <-- Not what I want

What I want is something like

echo $json['items']['snippet']['title'];

(But of course, that doesn't work)

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Spoody Apr 22 '18 at 23:39
  • in the `items` array, sometimes `$value` is an array, or an object, depending on the way you json_decode it. That is what the error message is telling you. You can try something like foreach items, then access the snippet as you want to do. – Félix Adriyel Gagnon-Grenier Apr 23 '18 at 00:11

1 Answers1

2

It sounds like you're simply looking for echo $json['items'][0]['snippet']['title'];.

If you want to loop over them, this can be done with array_walk():

function myfunction($value, $key)
{
    echo "The key $key has the value $value<br />";
}

array_walk($json, "myfunction");

Also note that your two lines:

echo $json['kind']."<p>".$json['etag']."<p>".$json['nextPageToken']."<p>";
echo $json['pageInfo']['totalResults']."<p>".$json['pageInfo']['resultsPerPage'];

Are invalid HTML; you cannot have a <p> tag nested within another <p> tag; don't forget to close them off with </p>.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Yes thank you! That's what I was looking for, and I can't believe I hadn't even thought of that. I feel like a herp derp. As to your second note, I appreciate it and yes I am aware. I'm just throwing out test code (not really worried about doing everything perfect atm) – Nicholette Liguori Apr 22 '18 at 23:42
  • @NicholetteLiguori accept this answer if this did helped you solve your problem, tick the check if its resolved – Kevin Apr 23 '18 at 01:01
  • Ah yes, I figured out the rest of what I needed too. Ty – Nicholette Liguori Apr 23 '18 at 01:16