PHP:Youtube Require YouTube.php 在第 32 行出现错误

标签 php youtube

fatal error :在第 32 行的/home/content/90/9753290/html/Google/Service/YouTube.php 中找不到类“Google_Service”

是我收到的错误消息,我从 GITHUB 得到的 GOOGLE API 东西,但我不知道这个错误消息是怎么回事???

如果我上传了 autoload.php 并添加了 require_once 'Google/autoload.php',我从某处听说过;它会摆脱错误,但仍然没有成功。

我试图按照 youtube 的谷歌教程来检索你自己的视频,但起点已经阻碍了我的进步

请问有什么帮助吗?

require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';

$OAUTH2_CLIENT_ID = 'xxx-xxx-xxx;
$OAUTH2_CLIENT_SECRET = 'xxx-xxx-xxx';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  try {
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'mine' => 'true',
    ));

    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];

      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $uploadsListId,
        'maxResults' => 13
      ));

      $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
      foreach ($playlistItemsResponse['items'] as $playlistItem) {
        $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
          $playlistItem['snippet']['resourceId']['videoId']);
      }
      $htmlBody .= '</ul>';
    }
  } catch (Google_ServiceException $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()));
  }

  $_SESSION['token'] = $client->getAccessToken();
} else {
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}

http://www.sentuamessage.com/index.php?action=snapclipstest我们可以看到错误

最佳答案

非常简短的回答:
将此添加到脚本的顶部,看看它是否有效:

set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/google-api-php-client/src');
(将 /path/to/google-api-php-client/src 更改为适当的路径)
简短的回答:
发生此错误是因为类 Google_Service未加载,这意味着:
  • 缺少文件(您没有正确复制库文件)
  • 文件存在但未加载(您的 autoload.php 脚本有问题)

  • 更长的答案:
    检查source code in the github repo ,错误报告回 Google/Service/YouTube.php 中的这段代码文件:
    class Google_Service_YouTube extends Google_Service
    {
    
    如您所见,它是 Google_Service_YouTube扩展类 Google_Service 的类声明.Google_Service类在 Goolge/Service.php 中定义文件。所以这个文件没有被正确加载。
    通过检查您的代码...
    require_once 'Google/autoload.php';
    require_once 'Google/Client.php';
    require_once 'Google/Service/YouTube.php';
    
    ..您正在使用库的自动加载器脚本,但随后手动需要其他文件,这很奇怪(而且您不应该需要它)!
    这可能意味着您没有正确遵循说明。 As stated in the documentation :

    After obtaining the files, ensure they are available to your code. If you're using Composer, this is handled for you automatically. If not, you will need to add the location of the src/ directory inside the client library to the include_path, so that the PHP runtime can find the files


    因此,由于您没有使用 Composer,因此您需要将库的路径添加到 php 的包含路径中。
    但是,我建议您使用 Composer安装这个库,as stated in this library's documentation .这将负责自动加载您的文件以及安装任何依赖项(目前没有,但将来可能会添加一些)。

    关于PHP:Youtube Require YouTube.php 在第 32 行出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28856547/

    相关文章:

    PHP 数组中的空键

    php - 带有连接和空白行的 mysqli 查询

    php - fopen 和 curl 之间最大的区别是什么?

    php - 多次更新的 MySQL 过程

    android - YouTube API v3示例IllegalArgumentException,Android Studio

    java - 如何在 YouTube API 中获取与一个 Google 帐户关联的多个用户名?

    php - $db = new DB() 混淆

    c# - YouTube HTTP请求

    android - 如何显示YouTube播放列表的 WebView ?

    php - Youtube 从 id 获取视频标题