dongwen7730 2018-04-22 23:35
浏览 43
已采纳

PHP json_decode youtube api(数组)[关闭]

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)

  • 写回答

1条回答 默认 最新

  • dozxos6346 2018-04-22 23:39
    关注

    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>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?