You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过YouTube Data API获取频道全量指定字段视频(突破500限制)

Solution: Fetch Full Channel Video List with Required Metadata via YouTube Data API

Great question! You’re right that the Search API isn’t the right tool for this job—its 500-result limit and lack of viewCount make it a poor fit for fetching a full channel's video list with the metadata you need. The good news is there's a fully compliant, official way to get exactly what you want using YouTube Data API's other endpoints, and it's actually more efficient than your current approach.

Step 1: Get the Channel’s Uploads Playlist ID

Every YouTube channel has a dedicated, auto-updating playlist for all its public uploads. First, we fetch this playlist ID using the Channels API:

  • API Endpoint:
    https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={CHANNEL_ID}&key={API_KEY}
    
  • Extract contentDetails.relatedPlaylists.uploads from the response—this is the unique ID for the channel's full upload history playlist.

Step 2: Fetch All Videos from the Uploads Playlist

Use the PlaylistItems API to iterate through the uploads playlist. Unlike the Search API, this endpoint has no hard 500-result limit (you can paginate through every video the channel has publicly uploaded):

  • API Endpoint (paginated):
    https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={UPLOAD_PLAYLIST_ID}&maxResults=50&pageToken={NEXT_PAGE_TOKEN}&key={API_KEY}
    
  • From each snippet object, you’ll get exactly the basic fields you need:
    • resourceId.videoId (your required videoId)
    • title
    • publishedAt

Step 3: Batch Fetch viewCount for All Videos

The PlaylistItems API doesn’t include video statistics, so we’ll batch up to 50 videoIds at a time and use the Videos API to pull viewCount efficiently:

  • API Endpoint (batch request):
    https://www.googleapis.com/youtube/v3/videos?part=statistics&id={COMMA_SEPARATED_VIDEO_IDS}&key={API_KEY}
    
  • Extract statistics.viewCount from each video’s response to add to your dataset.

Full PHP Example Code

Here’s a simplified script that ties all three steps together:

<?php
$apiKey = 'YOUR_API_KEY';
$channelId = 'TARGET_CHANNEL_ID';
$allVideos = [];

// Step 1: Retrieve the channel's uploads playlist ID
$channelsUrl = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={$channelId}&key={$apiKey}";
$channelsResponse = json_decode(file_get_contents($channelsUrl), true);
$uploadsPlaylistId = $channelsResponse['items'][0]['contentDetails']['relatedPlaylists']['uploads'];

// Step 2: Paginate through the uploads playlist to get all video metadata
$nextPageToken = '';
do {
    $playlistItemsUrl = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={$uploadsPlaylistId}&maxResults=50&pageToken={$nextPageToken}&key={$apiKey}";
    $playlistItemsResponse = json_decode(file_get_contents($playlistItemsUrl), true);
    
    // Collect basic video data
    foreach ($playlistItemsResponse['items'] as $item) {
        $videoId = $item['snippet']['resourceId']['videoId'];
        $allVideos[$videoId] = [
            'title' => $item['snippet']['title'],
            'publishedAt' => $item['snippet']['publishedAt']
        ];
    }
    
    $nextPageToken = $playlistItemsResponse['nextPageToken'] ?? '';
} while (!empty($nextPageToken));

// Step 3: Batch fetch view counts in groups of 50
$videoIds = array_keys($allVideos);
$batchSize = 50;
for ($i = 0; $i < count($videoIds); $i += $batchSize) {
    $batchIds = array_slice($videoIds, $i, $batchSize);
    $videosUrl = "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . implode(',', $batchIds) . "&key={$apiKey}";
    $videosResponse = json_decode(file_get_contents($videosUrl), true);
    
    // Attach view count to each video entry
    foreach ($videosResponse['items'] as $video) {
        $videoId = $video['id'];
        $allVideos[$videoId]['viewCount'] = $video['statistics']['viewCount'] ?? 0;
    }
}

// Output the final dataset
print_r($allVideos);
?>

Key Advantages of This Approach

  • No Result Limits: The uploads playlist includes every public video the channel has uploaded, so you can paginate through all of them (no 500-result cap like the Search API).
  • Quota Efficiency: This uses far fewer API quota units than repeated Search calls. Channels API uses 1 unit, PlaylistItems uses 1 per call, and Videos API uses 1 per batch call (regardless of how many IDs you pass).
  • Full Compliance: This is 100% within YouTube Data API’s terms of service—no scraping required, and all data is accessed through official, supported endpoints.

内容的提问来源于stack exchange,提问作者GFL

火山引擎 最新活动