YouTube APIを使って動画を取得してみたのでそれの覚え書きです。PHPでの基本的な使い方はsimplexml関数を使用して取得したデータをforeach文で出力していきます。なんかパラメータとか色々あって頭が混乱してきたので整理してみました。といっても初歩的なことですが。
[ads_center]
GETでリクエストしてデータを取得する
まずは以下のURLを変数に代入します。
$feedURL = "http://gdata.youtube.com/feeds/api/videos?vq=キーワード"
キーワードの部分は取得したい動画のキーワードを入れて下さい。もう少しパラメータを付け加えると色々と自分に合うデータを取得できます。例えば以下のようなパラメータを追加してみます。
$feedURL = "http://gdata.youtube.com/feeds/api/videos?vq=キーワード&max-results=5&start-index=1";
キーワードの後に追加してみました。詳細です。
- max-results
- 最大取得する動画の数です。上記の値は5なので最大で5件のデータを取得します。
- start-index
- 取得する開始データの番号です。上記では1としてるので1件目からのデータを取得します。
次にsimplexml_load_file関数を使って先程のURLを読み込みデータを取得します。
$sxml = simplexml_load_file($feedURL);
必要なデータを変数に格納していく
foreach文で必要なデータを取得していきます。
foreach($sxml->entry as $entry){
$media = $entry->children('http://search.yahoo.com/mrss/');
// タイトル
$title = $entry->title;
// 詳細
$desc = $media->group->description;
// URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// サムネイル
$content = $media->group->content->attributes();
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// 動画の時間(秒)
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
// 再生回数
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
// 評価
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
// ID
$arr = explode('/',$entry->id);
$id = $arr[count($arr)-1];
取得したデータを元に動画を出力する
以下上記の続きです。動画を出力していきます。
<p>タイトル:<?php echo $title; ?></p> <p>詳細:<?php echo $desc; ?></p> <p>URL:<?php echo $watch; ?></p> <p>時間:<?php echo $length; ?>秒</p> <p>再生回数:<?php echo $viewCount; ?></p> <p>評価:<?php echo $rating; ?></p> <p>ID:<?php echo $id; ?></p> <a href="<?php echo $content -> url; ?>"><img src="<?php echo $thumbnail; ?>" /></a> <hr />
とりあえず分かりやすいようにこんな感じに並べてみました。上記コード全部まとめるとこんな感じです。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>YouTube API</title>
</head>
<body>
<?php
$feedURL = "http://gdata.youtube.com/feeds/api/videos?vq=wordpress&max-results=5&start-index=1";
$sxml = simplexml_load_file($feedURL);
foreach($sxml->entry as $entry){
$media = $entry->children('http://search.yahoo.com/mrss/');
// タイトル
$title = $entry->title;
// 詳細
$desc = $media->group->description;
// URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// サムネイル
$content = $media->group->content->attributes();
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// 動画の時間 (秒)
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
// 再生回数
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
// 評価
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
// ID
$arr = explode('/',$entry->id);
$id = $arr[count($arr)-1];
?>
<p>タイトル:<?php echo $title; ?></p>
<p>詳細:<?php echo $desc; ?></p>
<p>URL:<?php echo $watch; ?></p>
<p>時間:<?php echo $length; ?>秒</p>
<p>再生回数:<?php echo $viewCount; ?></p>
<p>評価:<?php echo $rating; ?></p>
<p>ID:<?php echo $id; ?></p>
<a href="<?php echo $content -> url; ?>"><img src="<?php echo $thumbnail; ?>" /></a>
<hr />
<?php
}
?>
</body>
</html>
ブラウザで確認してみるとこんな感じです。

まだまだ数多くのパラメータがあり動画の出力形式も結構あると思うのでアイディア次第で色々と使い道がありそうですね。詳しい内容は公式ページに全部載ってるのでもっと知りたい人はこちらへどうぞ。
YouTubeのAPIとツール






