Youtube 在 API v3 PHP 中获取用户的视频 ID

Youtube get the videos IDs of user in API v3 PHP

我正在使用此代码在我的网站上显示用户的所有视频:

<?php
  $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/XXXXXXX/uploads');
  $server_time = $xml->updated;
  $return = array();
  foreach ($xml->entry as $video) {
      $vid = array();
      $vid['id'] = substr($video->id,42);
      $vid['title'] = $video->title;
      array_push($return, $vid);
  }

  ?>
<h2>Video Gallery</h2>
  <?php
  foreach($return as $video) {
    ?>
      <div class="col-md-4">
<div style="height: auto;" class="featured-box featured-box-secundary">
  <div class="box-content clearfix">
<h4><?= $video['title'] ?></h4>
<iframe width="270" height="203" src="https://www.youtube.com/embed/<?=$video['id']?>?rel=0&amp;showinfo=0" allowfullscreen></iframe>
      </div></div></div>
    <?php
  }
?>

但我收到了已弃用的通知。如何让 $vid['id'] 和 $vid['title'] 与 API v3 一起放入 HTML?

是的,Youtube API V2 已死。

要向 Youtube API V3 发出请求,您需要进行身份验证。获取 {YOUR_API_KEY}Google Developers Console. You'll need to create new project 并启用 Youtube API.

之后,您可以发出以下命令:

第一个

channels#list 方法将 return 一个 JSON 包含有关频道的一些信息,包括 "uploads" 播放列表的 {PLAYLIST_ID}

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={YOUR_API_KEY}

第二个

使用 {PLAYLIST_ID} 你可以通过 playlistItems#list 方法获取用户上传的视频:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={PLAYLIST_ID}&key={YOUR_API_KEY}

您可以在以下平台上测试以上代码:

Youtube apis-explorer