从youtube视频列表中获取数据

时间:2019-08-09 21:06:44

标签: php youtube-api

我想显示一个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"
   }
  }

1 个答案:

答案 0 :(得分:1)

YouTube数据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->项目,以列出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-> snippet-> publishedAt
  • YouTube频道ID – $ item-> snippet-> channelId
  • YouTube视频标题– $ item-> snippet->标题
  • YouTube视频描述– $ item-> snippet->说明
  • YouTube视频缩略图URL(默认大小)– $ item-> snippet-> thumbnails-> default-> url
  • YouTube视频缩略图URL(中等大小)– $ item-> snippet-> thumbnails-> medium-> url
  • YouTube视频缩略图URL(大尺寸)– $ item-> snippet-> thumbnails-> high-> url
  • YouTube频道标题– $ item-> snippet-> channelTitle