【问题标题】:Fetch data from youtube video list [closed]从 youtube 视频列表中获取数据 [关闭]
【发布时间】:2019-08-09 21:06:44
【问题描述】:

我想显示一个 youtube 频道的最新 5 个视频,包括标题、观看次数、缩略图等以进行分析。

但是我如何在显示第一个视频后显示下一个视频。因为没有 id=1 表示最新的,例如

所以我想展示第一个带有细节的视频,然后是第二个,等等。

我使用 PHP 和 HTML

{
 "kind": "youtube#searchListResponse",
 "etag": "\"nlUZBA6NbTS7q9G8D1GljyfTIWI/sqVszgEMcgndBQQW-8aekztyFWo\"",
 "nextPageToken": "CAUQAA",
 "regionCode": "DE",
 "pageInfo": {
  "totalResults": 313,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"nlUZBA6NbTS7q9G8D1GljyfTIWI/C8zU9hXFXvoOaiyzihj3QdaM3nk\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "eWTpXZ-sspk"
   },
   "snippet": {
    "publishedAt": "2018-08-28T16:30:11.000Z",
    "channelId": "UCzfu8Y5CsN7OCLJhZy_fH6w",
    "title": "RBX.Tools - Earn FREE Robux! (3,000 Giveaway)",
    "description": "Want to earn free robux? Our website allows you to do that easily without any limits! Check it out: https://rbx.tools Robux giveaway: ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/eWTpXZ-sspk/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/eWTpXZ-sspk/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/eWTpXZ-sspk/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "MRMURK4G3",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"nlUZBA6NbTS7q9G8D1GljyfTIWI/f-4ch1LXyzfYIL4utWuPz6CRFck\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "IVby2exqn1w"
   },
   "snippet": {
    "publishedAt": "2018-02-09T23:38:15.000Z",
    "channelId": "UCzfu8Y5CsN7OCLJhZy_fH6w",
    "title": "How To: Setup/Use Discord Webhooks! (PHP)",
    "description": "In this video i show you how to use discord webhooks for any custom event you would like using a simple cURL request in PHP. Find this video helpful in any ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/IVby2exqn1w/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/IVby2exqn1w/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/IVby2exqn1w/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "MRMURK4G3",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"nlUZBA6NbTS7q9G8D1GljyfTIWI/xq6mXfyCDDr8-C03ueN-4GhGP7U\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "QcD6RLYc34w"
   },
   "snippet": {
    "publishedAt": "2017-12-01T21:26:39.000Z",
    "channelId": "UCzfu8Y5CsN7OCLJhZy_fH6w",
    "title": "How To: Make A Qooh.Me Spammer! (C#)",
    "description": "In this video i show you how to make a simple spammer for anonymous question websites. Find this video helpful in any way? Be sure to drop a like & comment ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/QcD6RLYc34w/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/QcD6RLYc34w/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/QcD6RLYc34w/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "MRMURK4G3",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"nlUZBA6NbTS7q9G8D1GljyfTIWI/J8WfMLAVh58rILoataElddP5Q_o\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "iAwevpZK9Ug"
   },
   "snippet": {
    "publishedAt": "2017-11-19T19:21:23.000Z",
    "channelId": "UCzfu8Y5CsN7OCLJhZy_fH6w",
    "title": "Proxies24",
    "description": "Advertisement video, please click off if you're not interested! This is a short video to get a new site im a part of get started. Please let me know your thoughts on it.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/iAwevpZK9Ug/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/iAwevpZK9Ug/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/iAwevpZK9Ug/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "MRMURK4G3",
    "liveBroadcastContent": "none"
   }
  }

【问题讨论】:

    标签: php youtube-api


    【解决方案1】:

    YouTube Data API 请求返回包含视频信息(标题、描述、缩略图、发布日期等)的 JSON 数据。您需要指定 API 密钥 ($API_key)、YouTube 频道 ID ($channelID)。

    //Get videos from channel by YouTube Data API
    $API_key    = 'Insert_Your_API_Key';
    $channelID  = 'Insert_Channel_ID';
    $maxResults = 10;
    
    $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
    

    使用 PHP 的 Youtube 视频列表 遍历 $videoList->items 以列出 YouTube 频道中的视频。

    foreach($videoList->items as $item){
        //Embed video
        if(isset($item->id->videoId)){
            echo '<div class="youtube-video">
                    <iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                    <h2>'. $item->snippet->title .'</h2>
                </div>';
        }
    }
    

    在 YouTube 视频列表中,使用了视频 ID 和标题,但您可以根据需要显示其他信息。以下信息由 API 提供。

    • YouTube 视频 ID – $item->id->videoId
    • YouTube 视频发布日期 - $item->sn-p->publishedAt
    • YouTube 频道 ID – $item->sn-p->channelId
    • YouTube 视频标题 – $item->sn-p->title
    • YouTube 视频说明 – $item->sn-p->说明
    • YouTube 视频缩略图 URL(默认大小)– $item->sn-p->thumbnails->default->url
    • YouTube 视频缩略图 URL(中等大小)– $item->sn-p->thumbnails->medium->url
    • YouTube 视频缩略图 URL(大尺寸)– $item->sn-p->thumbnails->high->url
    • YouTube 频道标题 – $item->sn-p->channelTitle

    【讨论】:

    • 如果我想嵌入到我的 HTML 页面,我会得到: items as $item){ //Embed video if(isset($item->id->videoId)){ echo ' '. $item->sn-p->title 。 '; } } ?>
    猜你喜欢
    • 2017-08-25
    • 2012-10-24
    • 2015-08-23
    • 2015-11-28
    • 2021-12-19
    • 1970-01-01
    • 2015-08-31
    • 2013-09-12
    • 2017-09-01
    相关资源
    最近更新 更多