在youtube中如何获取我订阅的频道的最新上传视频(如v = ....的网址中的那个)的id,用于
在我的服务器端使用php 嵌入im
下面是使用YouTube RSS一个例子饲料,simplexml_load_file,parse_url,和parse_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.
Run Code Online (Sandbox Code Playgroud)
截至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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12780 次 |
| 最近记录: |