I am picking up video view count for a handful of videos from my own channel. This is the code I have just showing you 2 as an example:
<?php
define("YOUTUBE_DATA_API_KEY", 'KEY_IS_IN_HERE');
function youtube_video_statistics($video_id) {
$json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $video_id . "&key=". YOUTUBE_DATA_API_KEY );
$jsonData = json_decode($json);
$views = $jsonData->items[0]->statistics->viewCount;
return $views;
}
echo '<strong>Track Title 1 - </strong>';
$num = youtube_video_statistics('VIDEO_ID_HERE');
echo $formattedNum1;
$formattedNum1 = number_format($num);
echo $formattedNum1;
echo '<strong>Track Title 2</strong>';
$num = youtube_video_statistics('VIDEO_ID_HERE');
echo $formattedNum2;
$formattedNum2 = number_format($num);
echo $formattedNum2;
?>
The $formattedNum is just to format the numbers with apostrophes given a lot of my videos have views in the thousands.
This works fine but as you can see it is very manual. The track title and video ID is hardcoded in which isn't ideal.
What I want to do is pull the titles with view count in one batch of code that doesn't require me to keep updating it with a new ID once I add a new video etc. The caveat also is I have over 90 videos but it is only really 12 of them that I need to closely monitor the stats for but I guess if the only option to do this is to pull in the title and view count of every video on my channel, that's still better than my current position but I can't see any way to do even this in the docs.
So is there any way with the API to perhaps group the videos I want to monitor together so I can then query that particular group so for example use the same tag on them or add them to a playlist then query against that tag or playlist?
In reply to Benjamin Loison's answer, the code I get is just:
{
"kind": "youtube#videoListResponse",
"etag": "YIUPVpqNjppyCWOZfL-19bLb7uk",
"items": [],
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 0
}
}