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

PHP:Youtube Require Youtube.PHP在第32行出现错误,php,youtube,Php,Youtube,致命错误:在第32行的/home/content/90/9753290/html/Google/Service/YouTube.php中找不到类“Google_Service” 是我收到的错误消息,是我从GITHUB得到的GOOGLE API东西,但我不知道这个错误消息是怎么回事 我从某个地方听说我上传了autoload.php并添加了require_once“Google/autoload.php”;它可以消除错误,但仍然不能成功 我试着按照谷歌的youtube教程来检索你自己的视频,但起步阶

致命错误:在第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;
}
我们可以看到错误

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

set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/google-api-php-client/src');
(将
/path/更改为/google-api-php-client/src
更改为适当的路径)

简短答复: 发生此错误的原因是未加载类
Google\u服务
,这意味着:

  • 缺少文件(您没有正确复制库文件)
  • 文件存在但未加载(autoload.php脚本中有问题)
  • 详细回答: 检查时,错误会报告回文件中的此代码位:

    如您所见,是
    Google\u服务\u YouTube
    类声明扩展了类
    Google\u服务

    Google\u服务
    类在文件中定义。因此,此文件未正确加载

    通过检查您的代码

    require_once 'Google/autoload.php';
    require_once 'Google/Client.php';
    require_once 'Google/Service/YouTube.php';
    
    …您正在使用库的autoloader脚本,但需要手动使用其他文件,这很奇怪(您不应该需要它)

    这可能意味着您没有正确遵循说明:

    获取文件后,确保代码可以使用这些文件。如果 您使用的是Composer,它会自动为您处理。如果没有,, 您需要将src/目录的位置添加到 将客户端库添加到include_路径,以便PHP运行时可以找到 档案

    因此,因为您没有使用composer,所以需要将库的路径添加到php的include路径


    但是,我建议您使用来安装此库。这将负责自动加载您的文件以及安装任何依赖项(目前没有,但将来可能会添加一些依赖项)。

    第五行
    $OAUTH2\u CLIENT\u ID='xxx xxx(可能是由于复制/粘贴错误)是的,这是由于我编辑了xxx xxx xxx错误,或者“”确实存在,所以我在GITHUB上下载了zip文件,上传了它,编辑了代码以摆脱自动加载,并将其包含在顶部行中并编辑了路径,但我仍然收到了相同的错误,如果我尝试使用composer,我不会理解它:/我在自动加载中读取了您所说的所有内容,现在它工作了
    
    require_once 'Google/autoload.php';
    require_once 'Google/Client.php';
    require_once 'Google/Service/YouTube.php';