如何在youtube频道中获取最新上传视频的ID

时间:2011-07-07 21:50:53

标签: php youtube

在youtube中 我如何获取我订阅的频道中最新上传的视频(如v = ....的网址中的那个)的ID,用于嵌入 我在服务器端使用php

4 个答案:

答案 0 :(得分:8)

以下是使用YouTube RSS Feed,simplexml_load_fileparse_urlparse_str的示例。

<?php

$id = NULL;
$username = 'YouTube';

$xml = simplexml_load_file(sprintf('http://gdata.youtube.com/feeds/base/users/%s/uploads?alt=rss&v=2&orderby=published', $username));

if ( ! empty($xml->channel->item[0]->link) )
{
  parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);

  if ( ! empty($url_query['v']) )
    $id = $url_query['v'];
}

echo $id; // Outputs the video ID.

答案 1 :(得分:6)

截至2015年4月20日,看起来上述答案可能不再适用。以下是使用YouTube频道ID的示例(可在频道页面的源代码中找到。)

<?php

$id = NULL;
$channel_id = 'someChannelID';

$xml = simplexml_load_file(sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel_id));

if (!empty($xml->entry[0]->children('yt', true)->videoId[0])){
    $id = $xml->entry[0]->children('yt', true)->videoId[0];
}

echo $id; // Outputs the video ID.

答案 2 :(得分:1)

设置API密钥后,如何通过YouTube API执行此操作。

<?php

$channel_id = 'someChannelId';
$api_key = 'yourAPIKey';

$json_url="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$channel_id."&key=".$api_key;
$json = file_get_contents($json_url);
$listFromYouTube=json_decode($json);
$id = $listFromYouTube->items[0]->snippet->resourceId->videoId;

echo $id; // Outputs the video ID.

生成API密钥:

  1. 转到 - https://console.developers.google.com
  2. 创建新项目
  3. 选择“API&amp; auth”
  4. 选择“API”
  5. 选择“Youtube Data API v3”并启用它
  6. 选择“凭据”
  7. 创建新密钥,选择浏览器,然后按创建(不要输入 文本框中的任何内容)

答案 3 :(得分:0)

与Greg Brendel相同。 YT确实做了一些更改:“当我们升级YouTube数据API以带来更多功能时,我们将在2015年4月20日开始关闭旧版本。这将导致当前的YouTube应用无法在2012年的某些设备型号上运行和更老。“

https://support.google.com/youtube/answer/6098135?p=yt_devicesupport&hl=en&rd=1

相关问题