今回は特定のチャンネルで直近3ヶ月のランキングの取得方法です。
youtube APIキーの取得方法は割愛します。
<?php
//取得側
$PERIOD = date("Y-m-d",strtotime("-3 month")).'T00:00:00Z';
$DEVELOPER_KEY = "XXXXXXXAPIキーXXXXXXX";
$params = array(
"type" => "video",
"part" => "snippet",
"maxResults" => 3,
'regionCode' => 'JP',
'channelId' => 'XXXXXXXチャンネルIDXXXXXXX',
'order' => 'viewCount',
'publishedAfter' => $PERIOD,
"key" => $DEVELOPER_KEY,
);
$query = "";
foreach ($params as $key => $value) {
$query .= $key . "=" . $value . "&";
}
$query = substr($query, 0, -1);
$url = "https://www.googleapis.com/youtube/v3/search?".$query;
$result = file_get_contents($url);
$result = json_decode($result);
$rankingList = array();
foreach ($result->items as $key => $value) {
$rankingList[] = array(
"videoId" => $value->id->videoId,
"title" => $value->snippet->title,
"description" => $value->snippet->description,
"thumbnails_medium" => $value->snippet->thumbnails->medium->url,
);
}
?>
<?php
//出力側
echo $rankingList[0]["videoId"];
echo $rankingList[0]["title"];
echo $rankingList[0]["description"];
echo $rankingList[0]["thumbnails_medium"];
?>