PHPでGoogleAPIライブラリ(google/apiclient)を利用してYoutubeAPIを操作してみます
composerでライブラリのインストール
はい。これで完了
1 |
composer require google/apiclient |
Googleのデベロッパーコンソール
APIキーの作成
GoogleのデベロッパーコンソールでAPIを作成します
Google デベロッパーコンソール
- プロジェクトを作成
- 左側のメニューから「認証情報」をクリック
- 認証情報をクリック
- 「APIキーを作成」を選択
- 必要に応じて、アクセス制限をかけます
ここで作成したAPIキーをコピーしておきます
YoutubeAPIの有効化
- 「APIとサービスの有効化」をクリックするとAPIの検索画面が表示されます
- 「Youtube」で検索する
- 「YouTube Data API v3」が表示されたら「有効化」をクリック
これで完了
APIを利用するサンプル
公式ドキュメントに記載されている簡単な利用方法
get started
1 2 3 |
$client = new Google_Client(); $client->setApplicationName("My Application"); $client->setDeveloperKey("MY_SIMPLE_API_KEY"); |
Youtube API
YoutubeのAPIを利用して動画を検索してみます。認証していないので、認証が必要なAPIは利用できないです。単純な検索とか、動画再生くらいしかできません
YoutubeのAPIドキュメントはこちら
動画検索
動画を検索してみます。動画を検索するAPIのドキュメントはこちら
GoogleAPIライブラリでのYoutubeAPI利用
GoogleAPIライブラリを利用して検索してみます
この例では検索ワードとして「欅坂46」、チャンネルやプレイリストではなく、動画だけを対象にして、10件を検索します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
require_once (dirname(__FILE__) . '/vendor/autoload.php'); define("API_KEY","__YOUR_API_KEY_HERE__"); $client = new Google_Client(); $client->setApplicationName("xxxxx"); $client->setDeveloperKey(API_KEY); $youtube = new Google_Service_YouTube($client); $keyword = "欅坂46"; $params['q'] = $keyword; $params['type'] = 'video'; $params['maxResults'] = 10; $videos = []; try { $searchResponse = $youtube->search->listSearch('snippet', $params); array_map(function ($searchResult) use (&$videos) { $videos[] = $searchResult; },$searchResponse['items']); } catch (Google_Service_Exception $e) { echo htmlspecialchars($e->getMessage()); exit; } catch (Google_Exception $e) { echo htmlspecialchars($e->getMessage()); exit; } |
フォーム付きの全体コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php require_once (dirname(__FILE__) . '/vendor/autoload.php'); define("API_KEY","__YOUR_API_KEY_HERE__"); $client = new Google_Client(); $client->setApplicationName("xxxxxxxxxxx"); $client->setDeveloperKey(API_KEY); $youtube = new Google_Service_YouTube($client); $keyword = "欅坂46"; if( isset($_POST['keyword'])) { $keyword = $_POST['keyword']; } $params['q'] = $keyword; $params['type'] = 'video'; $params['maxResults'] = 10; $keyword = htmlspecialchars($keyword); $videos = []; try { $searchResponse = $youtube->search->listSearch('snippet', $params); array_map(function ($searchResult) use (&$videos) { $videos[] = $searchResult; },$searchResponse['items']); } catch (Google_Service_Exception $e) { echo htmlspecialchars($e->getMessage()); exit; } catch (Google_Exception $e) { echo htmlspecialchars($e->getMessage()); exit; } ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Youtube</title> <link rel="stylesheet" href="http://no1s.biz/kenshu/bootstrap/bootstrap-3.3.4/css/bootstrap.css"> <link rel="stylesheet" href="http://no1s.biz/kenshu/bootstrap/bootstrap-3.3.4/css/bootstrap-theme.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="http://no1s.biz/kenshu/bootstrap/bootstrap-3.3.4/js/bootstrap.min.js"></script> <style> html, body{ background-color: #f1f1f1; height: 100%; margin: 0; padding: 0; } #container{ height: 100%; } #header{ padding: 20px; background-color: #fff; } #header form{ width: 1200px; margin: 0 auto; } #main_box{ margin: 0 auto; width: 1200px; } .movieBox { margin: 20px 0; } a.movieLink { display:block; overflow: auto; padding: 10px; border: 1px solid #ccc; background-color: #fff; color: #696969; } a.movieLink:hover { border: 1px solid #4169E1; color: #4169E1; } .movieBox .thums { float: left; margin-right: 10px; width: 120px; } .movieBox .description { float:left; width: 1000px; word-break: break-all; } </style> </head> <body> <div id="container"> <div id="header"> <form method="post"> <input type="text" id="keyword" name="keyword" value="<?=$keyword?>" /> <input type="submit" value="検索" /> </form> </div> <div id="main_box" class="clearfix"> <?php foreach($videos as $video) : echo '<div class="movieBox">'; echo '<a class="movieLink" href="https://www.youtube.com/watch?v=' . $video['id']['videoId'] . '">'; echo '<div class="thums">'; echo '<img src="' . $video['snippet']['thumbnails']['default']['url']. '" />'; echo '</div>'; echo '<div class="description">'; echo '<div>' . $video['snippet']['title'] . '</div>'; echo '<div>' . $video['snippet']['description'] . '</div>'; echo '</div>'; echo '</a>'; echo '</div>'; endforeach; ?> </div> </div> </body> </html> |