2
3

More than 3 years have passed since last update.

[PHP]特定のYouTube動画の情報を取得する

Last updated at Posted at 2020-08-20

特定チャンネルの動画情報だったりプレイリスト一覧を取得したいとき、YouTubeAPIv3を使うと思います。
PHPだと、ComposerとかでGoogle公式ライブラリを使うのもいいですが特定の動画の場合はもっとかんたんに動画情報を取得できます。

具体的にはAPIから動画情報が格納されているJSONを呼び出します。
以上!

JSONのURL構造

https://www.googleapis.com/youtube/v3/videos?id=[動画ID]&key=[APIKey]&part=snippet,contentDetails,statistics,status

[APIKey]にはGoogleAPIから取得したAPIキーを入力してください。
キーの取得方法はネットに溢れかえってるんで自分で調べてください。

サンプルコード

YouTubeAPIv3から特定動画のタイトルを取得してみます。

yt.php
$api_key = "APIキーを入力"
$video_id = "動画IDを入力"
$url = "https://www.googleapis.com/youtube/v3/videos?id=$video_id&key=$api_key&part=snippet,contentDetails,statistics,status";
$json = file_get_contents($url);
$getData = json_decode( $json , true);
foreach((array)$getData['items'] as $key => $gDat){
    $title = $gDat['snippet']['title'];
}
// タイトルをechoする
echo $title;
2
3
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
2
3