YouTube Data API v3において定義されているリソースとPHPクライアントライブラリ (Google APIs Client Library for PHP) における実装がどのように対応しているか見ていきたい。
対応関係の概要だけでも知っておくと、APIリファレンスを読むとき、ドキュメント上での記述とPHPでアプリケーションを実装する際に必要なコードを頭の中で対応付けられるようになる。
APIリファレンスに記載されている通り、APIを利用して操作できるリソースには下記のものがある。
activities
リソースcaptions
リソースchannelBanners
リソースchannelSections
リソースchannels
リソースcommentThreads
リソースcomments
リソースguideCategories
リソースi18nLanguages
リソースi18nRegions
リソース(APIレファレンスに記載なし)liveBroadcasts
リソース(APIレファレンスに記載なし)liveChatBans
リソース(APIレファレンスに記載なし)liveChatMessages
リソース(APIレファレンスに記載なし)liveChatModerators
リソース(APIレファレンスに記載なし)liveStreams
リソースmembers
リソースmembershipsLevels
リソースplaylistItems
リソースplaylists
リソースsearch
リソース(APIレファレンスに記載なし)sponsors
リソースsubscriptions
リソース(APIレファレンスに記載なし)superChatEvents
リソースthumbnails
リソースvideoAbuseReportReasons
リソースvideoCategories
リソースvideos
リソースwatermarks
リソース
これらのリソースとPHPクライアントライブラリにおける実装の対応関係を見ていくために、簡単なコードを使って結果を確認していく。
まず、YouTube Data APIのリクエスト操作を行うには、下記のように Google_Service_YouTube
オブジェクトを作成する。
require_once '/path/to/google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
$client->setDeveloperKey("_API_KEY_");
$youtube = new Google_Service_YouTube($client);
ここで下記のコードを実行すると、$youtube
(Google_Service_YouTube
オブジェクト) が持つプロパティと、そのクラスを確認することができる。
$propertyNames = [
'activities',
'captions',
'channelBanners',
'channelSections',
'channels',
'commentThreads',
'comments',
'guideCategories',
'i18nLanguages',
'i18nRegions',
'liveBroadcasts',
'liveChatBans',
'liveChatMessages',
'liveChatModerators',
'liveStreams',
'members',
'membershipsLevels',
'playlistItems',
'playlists',
'search',
'sponsors',
'subscriptions',
'superChatEvents',
'thumbnails',
'videoAbuseReportReasons',
'videoCategories',
'videos',
'watermarks',
];
foreach ($propertyNames as $propertyName)
{
echo "property: ".'$youtube->'."$propertyName\n";
if (property_exists($youtube, $propertyName))
{
echo " ==> class : " . get_class($youtube->$propertyName) . "\n";
echo " ==> parent class: " . get_parent_class($youtube->$propertyName) . "\n\n";
}
else
{
echo " ==> not exists\n\n";
}
}
[実行結果] property: $youtube->activities ==> class : Google_Service_YouTube_Resource_Activities ==> parent class: Google_Service_Resource property: $youtube->captions ==> class : Google_Service_YouTube_Resource_Captions ==> parent class: Google_Service_Resource property: $youtube->channelBanners ==> class : Google_Service_YouTube_Resource_ChannelBanners ==> parent class: Google_Service_Resource property: $youtube->channelSections ==> class : Google_Service_YouTube_Resource_ChannelSections ==> parent class: Google_Service_Resource property: $youtube->channels ==> class : Google_Service_YouTube_Resource_Channels ==> parent class: Google_Service_Resource property: $youtube->commentThreads ==> class : Google_Service_YouTube_Resource_CommentThreads ==> parent class: Google_Service_Resource property: $youtube->comments ==> class : Google_Service_YouTube_Resource_Comments ==> parent class: Google_Service_Resource property: $youtube->guideCategories ==> class : Google_Service_YouTube_Resource_GuideCategories ==> parent class: Google_Service_Resource property: $youtube->i18nLanguages ==> class : Google_Service_YouTube_Resource_I18nLanguages ==> parent class: Google_Service_Resource property: $youtube->i18nRegions ==> class : Google_Service_YouTube_Resource_I18nRegions ==> parent class: Google_Service_Resource property: $youtube->liveBroadcasts ==> class : Google_Service_YouTube_Resource_LiveBroadcasts ==> parent class: Google_Service_Resource property: $youtube->liveChatBans ==> class : Google_Service_YouTube_Resource_LiveChatBans ==> parent class: Google_Service_Resource property: $youtube->liveChatMessages ==> class : Google_Service_YouTube_Resource_LiveChatMessages ==> parent class: Google_Service_Resource property: $youtube->liveChatModerators ==> class : Google_Service_YouTube_Resource_LiveChatModerators ==> parent class: Google_Service_Resource property: $youtube->liveStreams ==> class : Google_Service_YouTube_Resource_LiveStreams ==> parent class: Google_Service_Resource property: $youtube->members ==> class : Google_Service_YouTube_Resource_Members ==> parent class: Google_Service_Resource property: $youtube->membershipsLevels ==> class : Google_Service_YouTube_Resource_MembershipsLevels ==> parent class: Google_Service_Resource property: $youtube->playlistItems ==> class : Google_Service_YouTube_Resource_PlaylistItems ==> parent class: Google_Service_Resource property: $youtube->playlists ==> class : Google_Service_YouTube_Resource_Playlists ==> parent class: Google_Service_Resource property: $youtube->search ==> class : Google_Service_YouTube_Resource_Search ==> parent class: Google_Service_Resource property: $youtube->sponsors ==> class : Google_Service_YouTube_Resource_Sponsors ==> parent class: Google_Service_Resource property: $youtube->subscriptions ==> class : Google_Service_YouTube_Resource_Subscriptions ==> parent class: Google_Service_Resource property: $youtube->superChatEvents ==> class : Google_Service_YouTube_Resource_SuperChatEvents ==> parent class: Google_Service_Resource property: $youtube->thumbnails ==> class : Google_Service_YouTube_Resource_Thumbnails ==> parent class: Google_Service_Resource property: $youtube->videoAbuseReportReasons ==> class : Google_Service_YouTube_Resource_VideoAbuseReportReasons ==> parent class: Google_Service_Resource property: $youtube->videoCategories ==> class : Google_Service_YouTube_Resource_VideoCategories ==> parent class: Google_Service_Resource property: $youtube->videos ==> class : Google_Service_YouTube_Resource_Videos ==> parent class: Google_Service_Resource property: $youtube->watermarks ==> class : Google_Service_YouTube_Resource_Watermarks ==> parent class: Google_Service_Resource
実行結果を見ると、例えば $youtube
オブジェクトは、videos
リソースに対応する videos
プロパティを持ち、そのプロパティは Google_Service_YouTube_Resource_Videos
クラスのオブジェクトであることが分かる。また、各プロパティに対応するクラスは、全て Google_Service_Resource
クラスを親に持つことが分かる。
このことから、APIリファレンスに記載された videos
リソースに関する説明は、PHPでは $youtube
(Google_Service_YouTube
オブジェクト) の videos
プロパティに関する説明と解釈することができる。
次に、プロパティが持つメソッドを見ていきたい。下記のコードを実行すると、各プロパティが持つメソッドを確認することができる。
$methodNames = [
'list',
'insert',
'update',
'download',
'delete',
'markAsSpam',
'setModerationStatus',
'rate',
'getRating',
'reportAbuse',
'set',
'unset',
];
foreach ($propertyNames as $propertyName)
{
echo "property: ".'$youtube->'."$propertyName\n";
foreach ($methodNames as $methodName)
{
if (method_exists($youtube->$propertyName, $methodName))
{
echo " ==> {$methodName}()\n";
}
else
{
$PropertyName = $propertyName;
$PropertyName[0] = strtoupper($PropertyName[0]);
$methodNamePropertyName = $methodName . $PropertyName;
if (method_exists($youtube->$propertyName, $methodNamePropertyName))
{
echo " ==> {$methodNamePropertyName}()\n";
}
}
}
echo "\n";
}
[実行結果] property: $youtube->activities ==> listActivities() ==> insert() property: $youtube->captions ==> listCaptions() ==> insert() ==> update() ==> download() ==> delete() property: $youtube->channelBanners ==> insert() property: $youtube->channelSections ==> listChannelSections() ==> insert() ==> update() ==> delete() property: $youtube->channels ==> listChannels() ==> update() property: $youtube->commentThreads ==> listCommentThreads() ==> insert() ==> update() property: $youtube->comments ==> listComments() ==> insert() ==> update() ==> delete() ==> markAsSpam() ==> setModerationStatus() property: $youtube->guideCategories ==> listGuideCategories() property: $youtube->i18nLanguages ==> listI18nLanguages() property: $youtube->i18nRegions ==> listI18nRegions() property: $youtube->liveBroadcasts ==> listLiveBroadcasts() ==> insert() ==> update() ==> delete() property: $youtube->liveChatBans ==> insert() ==> delete() property: $youtube->liveChatMessages ==> listLiveChatMessages() ==> insert() ==> delete() property: $youtube->liveChatModerators ==> listLiveChatModerators() ==> insert() ==> delete() property: $youtube->liveStreams ==> listLiveStreams() ==> insert() ==> update() ==> delete() property: $youtube->members ==> listMembers() property: $youtube->membershipsLevels ==> listMembershipsLevels() property: $youtube->playlistItems ==> listPlaylistItems() ==> insert() ==> update() ==> delete() property: $youtube->playlists ==> listPlaylists() ==> insert() ==> update() ==> delete() property: $youtube->search ==> listSearch() property: $youtube->sponsors ==> listSponsors() property: $youtube->subscriptions ==> listSubscriptions() ==> insert() ==> delete() property: $youtube->superChatEvents ==> listSuperChatEvents() property: $youtube->thumbnails ==> set() property: $youtube->videoAbuseReportReasons ==> listVideoAbuseReportReasons() property: $youtube->videoCategories ==> listVideoCategories() property: $youtube->videos ==> listVideos() ==> insert() ==> update() ==> delete() ==> rate() ==> getRating() ==> reportAbuse() property: $youtube->watermarks ==> set() ==> unsetWatermarks()
実行結果を見ると、list
メソッド以外は、APIリファレンスに記載された通りの名前でメソッドが用意されている。list
メソッドについては、listVideos
のようにリソース名が付いた名前となっている。おそらく、これは、list()
がPHPにおいて特別な意味を持つキーワードとして登録されているので、混乱を避けるためにこのようなメソッド名を採用したと考えられる。
最後にAPIレスポンスについて見ていきたい。下記のコードを実行するとレスポンスに含まれるプロパティと、そのクラスを確認することができる。
$channelId = "UCrXUsMBcfTVqwAS7DKg9C0Q"; // YouTube Japan
$params = [
'id' => $channelId
];
$response = $youtube->channels->listChannels('snippet', $params);
echo "response\n";
echo " ==> class : " . get_class($response) . "\n";
echo " ==> parent class : " . get_parent_class($response) . "\n";
echo " ==> parent parent class: " . get_parent_class(get_parent_class($response)) . "\n\n";
echo "response.pageInfo\n";
echo " ==> class : " . get_class($response['pageInfo']) . "\n";
echo " ==> parent class: " . get_parent_class($response['pageInfo']) . "\n\n";
echo "// access as array\n";
foreach ($response['items'] as $item)
{
echo "item\n";
echo " ==> class : " . get_class($item) . "\n";
echo " ==> parent class: " . get_parent_class($item) . "\n\n";
echo "item.snippet\n";
echo " ==> class : " . get_class($item['snippet']) . "\n";
echo " ==> parent class: " . get_parent_class($item['snippet']) . "\n\n";
}
echo "// access with arrow operator\n";
foreach ($response->items as $item)
{
echo "item.snippet\n";
echo " ==> class : " . get_class($item->snippet) . "\n";
echo " ==> parent class: " . get_parent_class($item->snippet) . "\n\n";
}
echo "// just iterate over the object\n";
foreach ($response as $item)
{
echo "item.snippet\n";
echo " ==> class : " . get_class($item['snippet']) . "\n";
echo " ==> parent class: " . get_parent_class($item['snippet']) . "\n\n";
}
[実行結果] response ==> class : Google_Service_YouTube_ChannelListResponse ==> parent class : Google_Collection ==> parent parent class: Google_Model response.pageInfo ==> class : Google_Service_YouTube_PageInfo ==> parent class: Google_Model // access as array item ==> class : Google_Service_YouTube_Channel ==> parent class: Google_Model item.snippet ==> class : Google_Service_YouTube_ChannelSnippet ==> parent class: Google_Model // access with arrow operator item.snippet ==> class : Google_Service_YouTube_ChannelSnippet ==> parent class: Google_Model // just iterate over the object item.snippet ==> class : Google_Service_YouTube_ChannelSnippet ==> parent class: Google_Model
実行結果からAPIレスポンスに含まれるプロパティは、全て Google_Model
を継承したクラスのオブジェクトであることが分かる。
Google_Model
クラスは、ArrayAccess
インターフェイスを実装したクラスとなっているため、アロー演算子を使わず配列としてオブジェクトのプロパティにアクセスすることが可能となっている。
Google_Collection
クラスは Iterator
インターフェイスを実装したクラスで、反復処理では自動的に items
配列を参照するようになっているので、例えば、foreach
を使うとき、
foreach ($response['items'] as $item)
と明示的に items
配列を書かなくても、
foreach ($response as $item)
というように、単に $response
オブジェクトに対して foreach
を書くことができる。