6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-12-19

やりたいこと

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

手順

今回ニフティのC4SAでPHPキャンバスを立ち上げ、開発しました。

STEP1. Youtube API 利用する

Google developer consoleにアクセスし、もしアプリがない場合、以下のように作成してください!
youtube_api1.png
Youtube APIを開きます!Api Manager > Youtube APIs > Youtube Data API
youtube_api2.png
APIを有効にする
youtube_api3.png
APIキーを確認
youtube_api4.png

STEP2. PHP Libraryを利用する

たくさんありますが、今回試すのはGoogle API PHP Clientでした!
他のクライアント SDK はこちらで探せます.

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

youtube_api5.png

STEP3. 実装

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

<?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: 

参考

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?