2016-01-28 86 views
1

我使用Youtube v3 api,並且通過使用此調用獲取了所有類別的id。按類別搜索視頻id

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

現在我想獲得前5名的視頻從美國4類(音樂)。

什麼是正確的url語法?另外我怎樣才能使用Youtube api實例向它編寫一個搜索數組?

例子:

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

謝謝:)

回答

0

正如我實現了,這可能是helpful.The videoCategoryID音樂是10。你可以在這裏看到:

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

?>