【问题标题】:youtube api json phpyoutube api json php
【发布时间】:2016-02-28 00:51:00
【问题描述】:

我正在尝试使用 YouTube API 检索描述或任何数据,但它似乎对我不起作用 >.>

我在做什么:

    <?php

    $unparsed_json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=AIzaSyB-3Ayh1i6MlXXqWCr-NdJ07tepfalklfs&part=snippet,contentDetails,statistics,status");

    $json_object = json_decode($unparsed_json, true);

    ?>

    <html>
        <head>
            <title>Music Player</title>
            <link rel="stylesheet" href="/resources/app.css">
        </head>

        <body>
            <p><?php echo $json_object['items']['description']; ?></p>
        </body>
    </html>

API 输出的内容:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/HsO5dOfnOcgAuvefMmBwP_N0QX0\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/zt9_oZje8_2zsC7FjoJEVT0leuw\"",
   "id": "7lCDEYXw3mM",
   "snippet": {
    "publishedAt": "2012-06-20T23:12:38.000Z",
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
    "title": "Google I/O 101: Q&A On Using Google APIs",
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Google Developers",
    "tags": [
     "api",
     "gdl",
     "i-o"
    ],
    "categoryId": "28",
    "liveBroadcastContent": "none",
    "localized": {
     "title": "Google I/O 101: Q&A On Using Google APIs",
     "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0."
    }
   },
   "contentDetails": {
    "duration": "PT15M51S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "true",
    "licensedContent": false
   },
   "status": {
    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true
   },
   "statistics": {
    "viewCount": "6785",
    "likeCount": "39",
    "dislikeCount": "2",
    "favoriteCount": "0",
    "commentCount": "11"
   }
  }
 ]
}

我得到的错误是描述是一个未定义的索引。

【问题讨论】:

  • 那是因为$json_object['items'] 是一个数组,而不是一个结构。尝试循环遍历它,或者只是查看第一个条目:$json_object['items'][0] - 同样description 位于snippet 中,而不是在项目根目录中。

标签: php json youtube


【解决方案1】:

试试下面的代码:

<?php
$unparsed_json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=7lcdeyxw3mm&key=aizasyb-3ayh1i6mlxxqwcr-ndj07tepfalklfs&part=snippet,contentdetails,statistics,status");
$json_object = json_decode($unparsed_json, true);
?>

<html>
    <head>
        <title>music player</title>
        <link rel="stylesheet" href="/resources/app.css">
    </head>
    <body>
        <p><?php echo $json_object['items'][0]['snippet']['localized']['description']; ?></p>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2011-09-27
    • 2018-03-09
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多