php - youtube api json php

标签 php json youtube

我正在尝试使用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"
   }
  }
 ]
}

我得到的错误是描述是 undefined index 。

最佳答案

请尝试以下代码:

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

关于php - youtube api json php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677396/

相关文章:

php - 拉拉维尔 5.8 : $bal->balance is getting null

php - Azure 连接到 Windows 虚拟机 MySql 的 CodeIgniter

android - 使用 YouTube apk?

php - 将YouTube视频上传到AWS S3不能正常播放

php - 访问 AJAX 页面时如何显示未找到页面 (404)

php - [url] 的页面在 chrome 中运行了来自 [url] 的不安全内容

javascript - 使用 fetch() POST 将表单发送到本地服务器失败。错误405

sql - 如何解析postgres中文本列的json值

javascript - PHP stdclass对象返回给ajax

java - 如何使用 Jsoup 抓取 Youtube 视频的观看次数?