PHP: Youtube Require YouTube.php 在第 32 行发生错误

PHP: Youtube Require YouTube.php Occurs Error On Line 32

致命错误:在第 32

行的 /home/content/90/9753290/html/Google/Service/YouTube.php 中找不到 Class 'Google_Service'

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

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

我试图按照 YouTube 的 google 教程来检索您自己的视频,但起点已经阻碍了我的进步

有什么帮助吗?

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改成合适的路径)

简答:

发生此错误是因为 class Google_Service 未加载,这意味着:

  1. 一个文件丢失(你没有正确复制库文件)
  2. 文件存在但未加载(您的 autoload.php 脚本有问题)

更长的答案:

正在检查 source code in the github repo, the error reports back to this bit of code in the Google/Service/YouTube.php 文件:

class Google_Service_YouTube extends Google_Service
{

如您所见,Google_Service_YouTube class 声明扩展了 class Google_Service.

Google_Service class 在 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 to install this library, as stated in this library's documentation。这将负责自动加载您的文件以及安装任何依赖项(目前 none,但他们将来可能会添加一些)。