Reputation: 552
Unfortunately I need to ask someone that has found out how to do this because me and a friend could not achieve this in 8 hours of trying.
What we want to do: Just get the ratings of any given YouTube video (via its ID) and post these in an echo. We had this before YouTube switched to the API V3 and are now unable to get this to work in the new API.
How it worked in V2: We created a function that basically used the PHP API to set an array like this:
$rating[] = $videoEntry->getVideoRatingInfo();
How it is supposed to be working now: https://developers.google.com/youtube/v3/docs/videos/list#try-it here you will find a documentation and several examples (the PHP #1 didn't work, even though we fixed the error of
$youtube = new Google_Youtube_Service($client);
really being
$youtube = new Google_YoutubeService($client);
What does work?
try {
$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => $vid,
'maxResults' => 1,
));
foreach ($searchResponse['items'] as $searchResult) {
switch ($searchResult['id']['kind']) {
case 'youtube#video':
$sr = $searchResult['id']['videoId'];
$stit = $searchResult['snippet']['title'];
$svid = $searchResult['id']['videoId'];
This will eventually give us the title for a YouTube video, but unfortunately we can not get the ratings via search->listSearch, but only with videos->list or videos->listVideos (we are unsure of what is the correct one as the documentation isn't really accurate on this).
What we have tried: We basically tried dozens of combinations of this line (the one that is throwing the errors):
$videosResponse = $youtube->videos->list('part,id',array(
'part' => 'statistics'), array('id' => 'hoe9xW7vnpA'));
We are unsure of how to handle this correctly as the documentation doesn't tell us and the code example that fits best (PHP #1) did not work. We are also unsure what the errors want to tell us, here is an example:
An client error occurred: (list) missing required param: 'id' And another one here: Fatal error: Uncaught exception 'Google_Exception' with message '(list) missing required param: 'id'' in /kunden/406064_81373/webseiten/moone.in/test/google-api-php-client/src/service/Google_ServiceResource.php:117 Stack trace: #0 /kunden/406064_81373/webseiten/moone.in/test/google-api-php-client/src/contrib/Google_YouTubeService.php(810): Google_ServiceResource->__call('list', Array) #1 /kunden/406064_81373/webseiten/moone.in/yt.php(22): Google_VideosServiceResource->listVideos('hoe9xW7vnpA', Array, 'id,part') #2 {main} thrown in /kunden/406064_81373/webseiten/moone.in/test/google-api-php-client/src/service/Google_ServiceResource.php on line 117
Our testfiles can be found here: http://moone.in/yt.php http://moone.in/yttest.php http://moone.in/yttest2.php http://kati.renoi.de/test.php
If anyone could help us with the correct syntax to the video->videoList or ->list that would be awesome! The result should look like this:
{ "kind": "youtube#videoListResponse", "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/6A0cNjY9_rw5Yp9m7QtlTdTYMNw\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#video", "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/Y9W8NZu8IBcMlsQNsTsNLi5dk0k\"", "id": "hoe9xW7vnpA", "statistics": {
"viewCount": "7863",
"likeCount": "1105",
"dislikeCount": "10",
"favoriteCount": "0",
"commentCount": "785" } } ] }
Thank you!
Upvotes: 0
Views: 1690
Reputation: 552
http://kati.renoi.de/test.php?q=lightningsoul&location=&locationRadius=&maxResults=25
IT FINALLY WORKS!
Here is the WORKING code:
$DEVELOPER_KEY = 'CHANGE_TO_YOUR_OWN_GOOGLE_DEV_KEY';
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
// Define an object that will be used to make all API requests.
$youtube = new Google_YoutubeService($client);
try {
# Call the videos.list method to retrieve ratings for video with id hoe9xW7vnpA.
$videosResponse = $youtube->videos->listVideos('hoe9xW7vnpA','statistics',array(
'id' => 'hoe9xW7vnpA',
'part' => 'statistics',
));
$videos = '';
// Display the likes and dislikes of matching video.
foreach ($videosResponse['items'] as $videoResult) {
echo "likeCounts: ";
echo $videoResult['statistics']['likeCount'];
echo "<br />dislikeCounts: ";
echo $videoResult['statistics']['dislikeCount'];
}
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
Thanks to everyone believing in me: my mom, my brother, my girlfriend kati and all of you guys. ;)
Upvotes: 1