2013-07-11 73 views
6

我試圖用Youtube api 3.0獲取單個視頻的數據,但我無法弄清楚如何去做。youtube api 3.0獲取單個視頻的信息

我設法使這個例子工作很容易:https://developers.google.com/youtube/v3/code_samples/php

和定製它一下。

但現在我試圖獲取具有特定Id的單個視頻的信息。

這裏是我嘗試

$data = $youtube->videos->list("snippet"); 

,這總是給我這個錯誤

無法取消設定的字符串偏移量{}的server_path上/Google_ServiceResource.php線95

如果任何人都可以幫助我,我會很感激。

回答

1

我不知道你是否已經知道了 - 因爲它已經有一段時間了,但我相信你正在調用一個不存在的函數。我覺得應該是:

$data = $youtube->videos->listVideos("snippet"); 
+0

只是升技額外的幫助我:$ getSnippet = $ youtube-> videos-> listVideos( 「片斷」,陣列( '身份證'=> $ youTubeId)); –

10

無法給你確切的PHP代碼,但該網址是這樣的

String url = "https://www.googleapis.com/youtube/v3/videos?id="+videoID+"&key=" + YOUR KEY HERE + "&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics"; 

你得到你解析JSON對象:)

+0

我如何獲得視頻的擁有者 –

-1

確切的請求使用YouTube API第3版:

https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID_HERE&fields=items(snippet(title))&part=snippet&key=API_KEY_HERE 

,你會得到這樣的:

{ 
"items": [ 
    { 
    "snippet": { 
    "title": "VIDEO_TITLE_HERE" 
    } 
    } 
] 
} 
+0

您的答案與2014年@OWADVL給出的答案有何區別(除了格式不同?) –

+0

在他的回答中,您將獲得無用的附加信息。 –

0

使用AngularJS

app.controller('oneVideoCtrl', function($scope, $http, $stateParams, $location){ 

$scope.videos = []; 
$scope.youtubeParams = { 
    key: 'YOUR-API-KEY-HERE', 
    type: 'video', 
    kind: 'youtube#video', 
    maxResults: '1', 
    part: 'id,snippet', 
    order: 'viewCount', 
    channelId: 'YOUR-CHANNEL-ID-HERE', 
    playlistId: 'YOUR-PLAYLIST-ID-HERE', 
} 

// Get single specific YouTube video 
$http.get('https://www.googleapis.com/youtube/v3/videos?id=VIDEO-ID-HERE', {params:$scope.youtubeParams}).success(function(response){ 
    angular.forEach(response.items, function(child){ 
     console.log (child); 
     $scope.videos.push(child); 
    }); 
    }); 
});