按类别 ID 搜索视频


Searching videos by category id

我正在使用 Youtube v3 api,我使用此调用获得了所有类别 id。

$videoCategories = file_get_contents('https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode=us&key=xxxx');

现在我想从美国获得第 5 类(音乐)的前 4 个视频。

正确的网址语法是什么?还有如何使用 Youtube api 实例向其写入搜索数组?

例:

$searchResponse = $youtube->search->listSearch('id,snippet', array(
    'type' => 'video',
    // 'q' => $_GET['q'],
    'location' =>  'us',
    'maxResults' => 20,
));

谢谢:)

正如我实现的那样,这可能会有所帮助。音乐的videoCategoryID10 .你可以在这里看到这个:

https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=10&key={YOUR_API_KEY}所以你的代码最终看起来像这样:

$searchResponse = $youtube->search->listSearch('id,snippet', array(
    'q' => $_GET['q'],
    'maxResults' => $_GET['maxResults'],
    'type' => 'video',
    'videoCategoryId' => '10'
));

以下是获取完整类别列表的快速方法:

<?php
$ids = implode(",", range(1,50));
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=" . $ids . "&key={YOUR-API-KEY}");
$json_data = json_decode($JSON, true);
foreach ($json_data['items'] as $category) {
    echo $category['id'] . ' = ' . $category['snippet']['title'] . '<br/>';
}
?>