php - 使用YouTube API V3 PHP获得视频分级

标签 php video youtube youtube-api rating

不幸的是,我需要问一个已经找到了解决办法的人,因为我和一个 friend 在8小时的尝试中都无法做到这一点。

我们想要做什么:
只需获取任何给定YouTube视频的收视率(通过其ID)并将其发布在回显中即可。在YouTube切换到API V3之前,我们已经有了这个功能,现在无法在新的API中使用它。

在V2中的工作方式:
我们创建了一个函数,该函数基本上使用PHP API来设置如下数组:

$rating[] =  $videoEntry->getVideoRatingInfo();

现在应该如何工作:
https://developers.google.com/youtube/v3/docs/videos/list#try-it在这里您将找到一个文档和一些示例(即使我们修复了以下错误,PHP#1也不起作用。
  $youtube = new Google_Youtube_Service($client);

真的存在
  $youtube = new Google_YoutubeService($client);

有效吗?
   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'];

最终,这将为我们提供YouTube视频的标题,但是很遗憾,我们无法通过search-> listSearch来获得评分,而只能通过videos-> list或videos-> listVideos来获得评分(我们不确定哪种是正确的文档对此并不十分准确)。

我们尝试了什么:
我们基本上尝试了这一行的数十种组合(抛出错误的组合):
$videosResponse = $youtube->videos->list('part,id',array(
'part' => 'statistics'), array('id' => 'hoe9xW7vnpA'));

我们不确定如何正确处理此问题,因为文档没有告诉我们并且最合适的代码示例(PHP#1)无法正常工作。我们也不确定错误要告诉我们什么,这是一个示例:

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



我们的测试文件可以在这里找到:
http://moone.in/yt.php http://moone.in/yttest.php http://moone.in/yttest2.php
http://kati.renoi.de/test.php

如果有人可以帮助我们为video-> videoList或-> list提供正确的语法,那就太棒了! 结果应如下所示:
{  "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"    }   }  ] }

谢谢!

最佳答案

http://kati.renoi.de/test.php?q=lightningsoul&location=&locationRadius=&maxResults=25

终于可以了!

这是工作代码:

  $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()));
  }

多亏每个相信我的人:我的妈妈,我的兄弟,我的女友卡蒂和你们所有人。 ;)

关于php - 使用YouTube API V3 PHP获得视频分级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581215/

相关文章:

c++ - VideoOutput QML 的自定义源属性

java - 捕获/导出 JOGL 动画到视频文件

javascript - 结束最后一个视频youtube API后如何获取第一个视频

iframe - 延迟加载 youtube iframe 但继续关注播放/暂停按钮?

php - 我应该检查 pdo php 中执行操作的返回值吗

php - Codeigniter数据库删除和插入多条记录

javascript - 视频内联 iOS 不工作

backbone.js - 使用handlebars.js渲染youtube搜索数据时遇到问题

javascript - onBlur() 函数在 JavaScript 中不起作用

php - MYSQL IN 子句错误