2015-07-05 43 views
2

我試圖僅使用api過濾音樂視頻。youtube api僅限音樂過濾器

我看了YouTube上的api(v3)網站,但無法找到它。

try { 
     // Call the search.list method to retrieve results matching the specified 
     // query term. 
     $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'], 
      'maxResults' => $_GET['maxResults'], 
      'type' => 'video', 
      **'videoCategoryId' => 'Music',** 
     )); 

回答

2

videoCategoryIDmusic10。你可以在這裏看到:

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/>'; 
} 

?>