Youtube API v.3 を使って、ビデオの詳細（タイトル、サムネイルなど）を取得（PHP版）

# やりたいこと

PHPでYoutubeの動画を自分用に作りたい！
ただ再生だけではなく、タイトルやカテゴリー、サムネイルも入れるので、動画のリストなどでも使えそう

# 手順
今回ニフティの[C4SA](http://c4sa.nifty.com/)でPHPキャンバスを立ち上げ、開発しました。
* Youtube API 利用準備
* PHP Libraryを利用する　→　[Google API PHP Client](https://github.com/google/google-api-php-client/releases)
* 実装

# STEP1. Youtube API 利用する

[Google developer console](https://console.developers.google.com)にアクセスし、もしアプリがない場合、以下のように作成してください！
![youtube_api1.png](https://qiita-image-store.s3.amazonaws.com/0/18698/ba9391f6-7eeb-9750-a276-e904a1bf2e03.png)
Youtube APIを開きます！Api Manager > Youtube APIs > Youtube Data API
![youtube_api2.png](https://qiita-image-store.s3.amazonaws.com/0/18698/b389ec7a-bb57-c80c-f44c-5d4ecd411a9f.png)
APIを有効にする
![youtube_api3.png](https://qiita-image-store.s3.amazonaws.com/0/18698/76153cb3-18ba-c20d-c586-f0dda500884a.png)
APIキーを確認
![youtube_api4.png](https://qiita-image-store.s3.amazonaws.com/0/18698/bd2cf629-2946-9ccd-48f7-9bd2b90df350.png)


# STEP2.  PHP Libraryを利用する

たくさんありますが、今回試すのは[Google API PHP Client](https://github.com/google/google-api-php-client/releases)でした！
他のクライアント SDK は[こちら](https://developers.google.com/youtube/v3/libraries?hl=ja)で探せます.

SDKをダウンロードし、/src/Googleフォルダーをコピーし、C4SAキャンバスにアップします。
ファイルをアップロードする際、１個ずつアップするのは大変なので、WEBDAVを利用することはおすすめ！

![youtube_api5.png](https://qiita-image-store.s3.amazonaws.com/0/18698/5d241649-c77f-1728-73e5-e4ccc1b81e75.png)

# STEP3. 実装

以下のように、APIKEYを渡し、video_id (https://www.youtube.com/watch?v=video_id)の詳細が帰ってきた！

```php
<?php

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

$client = new Google_Client();
$client->setDeveloperKey('APIKEY'); 
$youtube = new Google_Service_YouTube($client);

$videoResponse = $youtube->videos->listVideos('snippet', array(
    'id' => 'video_id' 
));

var_dump($videoResponse);
?>

<!doctype html>
<html>
  <head>
    <title>Video information</title>
  </head>
  <body>
  </body>
 </html>
```

サンプル結果

```
object(Google_Service_YouTube_VideoListResponse)#32 (16)
 { ["collection_key":protected]=> string(5) "items" ["internal_gapi_mappings":protected]=> array(0) { } ["etag"]=> string(57) ""kuL0kDMAqRo3pU7O0pwlO-Lfzp4/jV0pxPvqP4D7secYwO-iRDwut9c"" ["eventId"]=> NULL ["itemsType":protected]=> string(28) "Google_Service_YouTube_Video" ["itemsDataType":protected]=> string(5) "array" ["kind"]=> string(25) "youtube#videoListResponse" ["nextPageToken"]=> NULL ["pageInfoType":protected]=> string(31) "Google_Service_YouTube_PageInfo" ["pageInfoDataType":protected]=> string(0) "" ["prevPageToken"]=> NULL ["tokenPaginationType":protected]=> string(38) "Google_Service_YouTube_TokenPagination" ["tokenPaginationDataType":protected]=> string(0) "" ["visitorId"]=> NULL ["modelData":protected]=> array(2) { ["pageInfo"]=> array(2) { ["totalResults"]=> int(1) ["resultsPerPage"]=> int(1) } ["items"]=> array(1) { [0]=> array(4) { ["kind"]=> string(13) "youtube#video" ["etag"]=> string(57) ""kuL0kDMAqRo3pU7O0pwlO-Lfzp4/hb6qXcCz2UE758hgZyp6lnnxo5o"" ["id"]=> string(11) "YUAmi7Q2F0Y" ["snippet"]=> array(10) { ["publishedAt"]=> string(24) "2015-12-04T10:26:08.000Z" ["channelId"]=> string(24) "UC9c3qUdRWmMic4-5yTjvCNA" ["title"]=> string(37) "THẬT BẤT NGỜ (MV) - TRÚC NHÂN" ["description"]=> string(539) "_ca khúc: Thật Bất Ngờ _sáng tác: MewAmazing -https://www.facebook.com/mewamazing?fref=ts _Ca sỹ: Trúc Nhân - https://www.facebook.com/truc.nhan.7 _Đạo diễn: Nhu Đặng - https://www.facebook.com/nhudang.colorista?fref=ts _Quay phim: Ứng Duy Kiên - https://www.facebook.com/kienung92?fref=ts _Sản xuất: Ngô Đài Trang - https://www.facebook.com/profile.php?id=100000094125024&fref=ts _Link load Game "Khu vườn trên mây": http://kvtm.vn/?utm_source=trucnhan-thatbatngo&utm_medium=youtube&utm_campaign=tn-tbn" ["thumbnails"]=> array(5) { ["default"]=> array(3) { ["url"]=> string(46) "https://i.ytimg.com/vi/YUAmi7Q2F0Y/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/YUAmi7Q2F0Y/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/YUAmi7Q2F0Y/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } ["standard"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/YUAmi7Q2F0Y/sddefault.jpg" ["width"]=> int(640) ["height"]=> int(480) } ["maxres"]=> array(3) { ["url"]=> string(52) "https://i.ytimg.com/vi/YUAmi7Q2F0Y/maxresdefault.jpg" ["width"]=> int(1280) ["height"]=> int(720) } } ["channelTitle"]=> string(11) "Trúc Nhân" ["tags"]=> array(7) { [0]=> string(9) "truc nhan" [1]=> string(11) "Trúc Nhân" [2]=> string(15) "MV that bat ngo" [3]=> string(21) "MV Thật Bất Ngờ" [4]=> string(20) "Official Music Video" [5]=> string(17) "nhac tre hay 2015" [6]=> string(16) "young teen music" } ["categoryId"]=> string(2) "10" ["liveBroadcastContent"]=> string(4) "none" ["localized"]=> array(2) { ["title"]=> string(37) "THẬT BẤT NGỜ (MV) - TRÚC NHÂN" ["description"]=> string(539) "_ca khúc: Thật Bất Ngờ _sáng tác: MewAmazing -https://www.facebook.com/mewamazing?fref=ts _Ca sỹ: Trúc Nhân - https://www.facebook.com/truc.nhan.7 _Đạo diễn: Nhu Đặng - https://www.facebook.com/nhudang.colorista?fref=ts _Quay phim: Ứng Duy Kiên - https://www.facebook.com/kienung92?fref=ts _Sản xuất: Ngô Đài Trang - https://www.facebook.com/profile.php?id=100000094125024&fref=ts _Link load Game "Khu vườn trên mây": http://kvtm.vn/?utm_source=trucnhan-thatbatngo&utm_medium=youtube&utm_campaign=tn-tbn" } } } } } ["processed":protected]=> array(0) { } } Title: 
```

# 参考

* https://developers.google.com/youtube/v3/libraries?hl=ja
* http://stackoverflow.com/questions/30769747/get-youtube-title-from-videoid-in-php-using-api-v3