我已经上传了大约 100 个视频到我的频道,我需要获取所有视频 ID,但是我搜索到哪里都有只列出播放列表内视频的代码,但我的所有视频都在 root 中。
$channelsResponse = $youTubeService->channels->listChannels('id,contentDetails', array(
'mine' => 'true'));
$searchResponse = $youTubeService->playlistItems->listPlaylistItems('snippet', array(
'maxResults' => 50,
'fields' => 'items(snippet(publishedAt,channelId,title,description,thumbnails(default),resourceId)),pageInfo,nextPageToken'));
echo json_encode($searchResponse['items']['contentDetails']['videoId']);
上面的代码从播放列表中获取视频。如何从根目录获取视频。
没有什么比root更好的了,一切都在YouTube的播放列表中。我相信您所要求的是获取您上传的视频。您的频道中还有一个名为"上传"的播放列表。这是您的频道上传视频添加的地方。
您可以在列表频道调用中获取该播放列表的播放列表ID。在响应中,查找 contentDetails.relatedPlaylists.uploads。
然后在第二次调用(listPlaylistItems)中,使用该ID设置播放列表ID
下面是 PHP 示例。
工作解决方案/示例:
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//params
$order = "date";
$part = "snippet";
$channel_id = "UCzocxfp9lrNal14glrFSrng";
$max_results = 50;
$api_key = "YOUR_API_KEY";
$feedurl = 'https://www.googleapis.com/youtube/v3/search?order='.$order.'&part='.$part.'&channelId='.$channel_id.'&maxResults='.$max_results.'&key='.$api_key;
$feed = getSslPage($feedurl);
$feed_arr = json_decode($feed, true);
//print_r($feed_arr);
$items = $feed_arr['items'];
foreach($items as $item) {
if( (isset($item['id']['videoId'])) OR (!empty($item['id']['videoId'])) ) {
echo '<iframe width="420" height="315"
src="http://www.youtube.com/embed/'.$item['id']['videoId'].'?autoplay=0">
</iframe>';
}
}