ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
エラーメッセージ
PHP Fatal error: Uncaught Error: Class 'Google_Service_Youtube' not found
in /var/www/app.php:5
Stack trace:
#0 {main}
thrown in /var/www/app.php on line 5
Google_Service_Youtube (または Google_Service_YouTube 等)は、Google API Client Library for PHP に含まれるYouTube Data API v3 用クラスです。「クラスが見つからない」のは、ほぼautoload / インストール / 名前空間のいずれかの問題です。
原因と対処の早見表
| 原因 | 確認 | 対処 |
|---|---|---|
| パッケージ未インストール | composer show google/apiclient | composer require google/apiclient:^2.0 |
| vendor/autoload.php 未読込 | エントリポイントを確認 | require "vendor/autoload.php" 追加 |
| autoload キャッシュ古い | vendor/composer/autoload_*.php | composer dump-autoload -o |
| v2.x の名前空間ミス | クライアントライブラリ版数 | Google\Service\YouTube に変更 |
| YouTube API 無効 | GCP コンソールで確認 | API ライブラリで有効化 |
| service クラスのみ別パッケージ | v2.10.0+ で分離 | google/apiclient-services も必要 |
対処1: インストール
# パッケージ確認
composer show | grep google
# google/apiclient v2.15.0
# google/apiclient-services v0.331.0
# google/auth v1.39.0
# 未インストールの場合
composer require google/apiclient:^2.15.0
# Laravel 12 / PHP 8.2+ の場合は最新版
composer require google/apiclient
対処2: autoload を読み込む
<?php
// ★ 必ずスクリプト先頭で読み込む
require_once __DIR__ . '/vendor/autoload.php';
// Laravel / Symfony なら自動で読まれているので不要
// 直接書く PHP スクリプト・CLI ツールでは必須
$client = new Google\Client();
$client->setDeveloperKey('YOUR_API_KEY');
$youtube = new Google\Service\YouTube($client);
対処3: autoload キャッシュ再生成
# autoload マップを再生成
composer dump-autoload
# 本番では -o で classmap 最適化
composer dump-autoload -o
# プロダクション準拠
composer install --optimize-autoloader --no-dev
対処4: 名前空間の変更(v2.x の重要な変更)
Google API Client Library v2.7.0 以降、クラス名がアンダースコア区切りからモダンな名前空間へ移行しました。古い記事の通り書くと動きません:
// ❌ 旧式(v2.7.0 未満)
$client = new Google_Client();
$client->setDeveloperKey('YOUR_API_KEY');
$youtube = new Google_Service_YouTube($client);
$response = $youtube->search->listSearch('snippet', [
'q' => 'PHP tutorial',
'maxResults' => 10,
]);
// ✅ 新式(v2.7.0+, v2.x 推奨)
$client = new Google\Client();
$client->setDeveloperKey('YOUR_API_KEY');
$youtube = new Google\Service\YouTube($client);
$response = $youtube->search->listSearch('snippet', [
'q' => 'PHP tutorial',
'maxResults' => 10,
]);
// ※ 旧クラス名はエイリアスとして残存(廃止予定)
// → 警告は出ないが将来的に削除される可能性あり
| 旧クラス名 | 新クラス名 |
|---|---|
Google_Client | Google\Client |
Google_Service_YouTube | Google\Service\YouTube |
Google_Service_YouTube_SearchListResponse | Google\Service\YouTube\SearchListResponse |
Google_Service_Drive | Google\Service\Drive |
Google_Service_Sheets | Google\Service\Sheets |
Google_Auth_OAuth2 | Google\Auth\OAuth2 |
対処5: GCP コンソールで API 有効化
クラスは読み込めても API が無効だと 403 PERMISSION_DENIED が返ります:
- GCP コンソールにログイン
- プロジェクトを選択(または新規作成)
- 「APIとサービス」→「ライブラリ」
- 「YouTube Data API v3」を検索
- 「有効にする」をクリック
- 「認証情報」から API キー or OAuth2 クライアント ID を作成
対処6: services パッケージが別建てになっている
v2.10.0 以降、サービス定義クラスは google/apiclient-services という別パッケージに分離されました。通常は自動依存で入りますが、念のため確認:
composer show google/apiclient-services
# v0.331.0
# 不足していたら個別インストール
composer require google/apiclient-services
完全動作するサンプル
<?php
require __DIR__ . '/vendor/autoload.php';
// 1. クライアント作成
$client = new Google\Client();
$client->setApplicationName('My YouTube Search App');
$client->setDeveloperKey('YOUR_API_KEY'); // GCP コンソールで発行
// 2. YouTube サービス
$youtube = new Google\Service\YouTube($client);
// 3. 動画検索
try {
$searchResponse = $youtube->search->listSearch('snippet', [
'q' => 'Laravel tutorial',
'maxResults' => 5,
'type' => 'video',
]);
foreach ($searchResponse->items as $item) {
echo $item->id->videoId . " : " . $item->snippet->title . "\n";
}
} catch (Google\Service\Exception $e) {
error_log('API error: ' . $e->getMessage());
} catch (Google\Exception $e) {
error_log('Client error: ' . $e->getMessage());
}
Laravel での組み込み
// composer require google/apiclient
// .env に YOUTUBE_API_KEY=xxx
// config/services.php
'youtube' => [
'key' => env('YOUTUBE_API_KEY'),
],
// Service クラス
namespace App\Services;
class YouTubeService
{
private \Google\Service\YouTube $youtube;
public function __construct()
{
$client = new \Google\Client();
$client->setDeveloperKey(config('services.youtube.key'));
$this->youtube = new \Google\Service\YouTube($client);
}
public function search(string $query, int $maxResults = 10): array
{
$res = $this->youtube->search->listSearch('snippet', [
'q' => $query,
'maxResults' => $maxResults,
'type' => 'video',
]);
return $res->items;
}
}
// Controller
class VideoController extends Controller
{
public function index(YouTubeService $yt)
{
$items = $yt->search('Laravel 11');
return view('videos.index', compact('items'));
}
}
類似エラー
| エラー | 対処 |
|---|---|
Class 'Google\Client' not found | 同根。autoload + 名前空間確認 |
Class 'Google_Service_Drive' not found | 同根。Drive も同じパッケージ |
Class 'Google_Service_Sheets' not found | 同根 |
Class 'Google_Auth_AssertionCredentials' not found | v2 で削除済。サービスアカウントは setAuthConfig() + JSON ファイル |
quotaExceeded | YouTube API のクォータ消費上限。GCP コンソールで増加申請 |
403 The request cannot be completed because you have exceeded your quota | クォータ枯渇 |
FAQ
Q: 旧 v1 系 (Google API Client v1) を使っている
A: v1 は 2018 年に EOL。v2 へ移行してください。composer require google/apiclient:^2.0。
Q: composer.json に書いてあるのにクラスが見つからない
A: composer install または composer update が走っていません。さらに composer dump-autoload。
Q: PHP のバージョンが古い
A: 最新の google/apiclient は PHP 8.0+ が必要。古い PHP では古い版数を指定: composer require google/apiclient:^2.12.6。
Q: API キーで動かない、OAuth が必要?
A: 検索などの公開データのみなら API キーで OK。ユーザーのチャンネル操作 / アップロードは OAuth2 必須。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページはありません
- Fatal error: Maximum execution time of 30 seconds exceeded in...
- Fatal error: Uncaught Error: Cannot use object of type stdClass as array in ...
- Warning: Use of undefined constant ... - assumed '...' (this will throw an Error)
- ERROR: Call to undefined method Maatwebsite\Excel\Excel::load()
- Maximum execution time of 30 seconds exceeded
- Your requirements could not be resolved to an installable set of packages. ... To enable extensions, verify that they are enabled in your .ini files:
- could not find driver
- the requested PHP extension mbstring is missing from your system.
- the requested PHP extension dom is missing from your system.
- A non well formed numeric value encountered
- Warning: Cannot modify header information - headers already sent by ...
- php_network_getaddresses: getaddrinfo failed: Name or service not known
- XMLWriter::openUri(): Unable to resolve file path
- Object of class stdClass could not be converted to string
- Class 'Google_Service_Youtube' not found
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- IPv6とは|128bitアドレス・コロン16進表記/::省略・リンクローカル・SLAAC・デュアルスタック NEW 2026-06-22 12:34:44
- MAC アドレスフィルタリングの仕組みと限界 | ネットワーク入門 NEW 2026-06-22 12:19:10
- VPNとは|暗号トンネル・サイト間/リモートアクセス・IPsec/SSL-VPN/WireGuardを解説 NEW 2026-06-22 12:19:10
- WebRTC とは ブラウザ間 P2P の音声・映像・データ通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/2 とは 多重化・HPACK・バイナリフレーム | ネットワーク入門 NEW 2026-06-22 12:17:25
- Web通信プロトコル入門 HTTP/2・HTTP/3・WebSocket・gRPC・WebRTC | ネットワーク入門 NEW 2026-06-22 12:17:25
- gRPC とは HTTP/2 + Protocol Buffers の高速 RPC | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/3 (QUIC) とは UDP ベースの低遅延 Web 通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- WebSocket とは 全二重リアルタイム通信 ws/wss | ネットワーク入門 NEW 2026-06-22 12:17:25
- 証明書と認証局(CA)とは|X.509・信頼チェーン・DV/OV/EV・失効(CRL/OCSP)を解説 NEW 2026-06-22 12:17:24
- ファイアウォールとは|パケットフィルタ・ステートフル・DMZ・次世代FW(L4/L7)を解説 NEW 2026-06-22 12:17:24
- iptables/nftablesとは|テーブル・チェーン・ルール例・永続化をLinux視点で解説 NEW 2026-06-22 12:17:24
- HAProxy とは frontend/backend と設定例 | ネットワーク入門 NEW 2026-06-22 12:17:24
- CDN とは エッジキャッシュ・TTL・Cloudflare/CloudFront | ネットワーク入門 NEW 2026-06-22 12:17:24
- TLS/SSLの仕組み|ハンドシェイク・暗号スイート・前方秘匿性・証明書検証をわかりやすく解説 NEW 2026-06-22 12:17:24
コメントを削除してもよろしいでしょうか?