2016-01-29 113 views
0

我需要幫助與YouTube API V3。 當我在瀏覽器上輸入: https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key= 它顯示返回值。 但是我試圖從php收集數組。如何使用YouTube的API V3

如何在PHP中使用GET https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key=以便在php中獲得結果?

或者是否有任何選項以rss提要格式獲取搜索結果。

在此先感謝

回答

0

在YouTube上的API一個簡單的視頻搜索,你會得到視頻的標題,描述,視頻ID和圖像源,最好的辦法完整的代碼如下。

<?php 

error_reporting(0); 

$search = "Search Query"; // Search Query 

$api = "YouTube API Key"; // YouTube Developer API Key 

$results = 10; // Max results 

$link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=$results&key=". $api; 

$video = file_get_contents($link); 

$video = json_decode($video, true); 

foreach ($video['items'] as $data){ 
     $title = $data['snippet']['title']; 
     $description = $data['snippet']['title']; 
     $vid = $data['id']['videoId']; 
     $image = "https://i.ytimg.com/vi/$vid/default.jpg"; 

     // Output Title/Description/Image URL If Video ID exist 
     if($vid){ 
      echo "Title: $title<br />Description: $description<br />Video ID: $vid<br />Image URL: $image<hr>"; 
     } 
} 
?> 
+0

搜索視頻(相關性)使用這個網址,你可以得到VideoID的和標題,圖片的網址等https://www.googleapis.com/youtube/v3/search?part=snippet&contentdetails&q=SEARCH KEY&類型=視頻和videoSyndicated = true&key =您的API密鑰&maxResults = 25 – Yogendra

+0

https://www.googleapis.com/youtube/v3/search?part=snippet&contentdetails&q={SERCH KEY}&type = video&videoSyndicated = true&key = {YOUR API KEY}&maxResults = 25 – Yogendra