jacquesdegatineau Posted August 21, 2022 Share Posted August 21, 2022 Hello. I want to display one View Count of multiple YouTube videos on my website. I'm not very tech-savvy, but here's a PHP code that I found on this website. With this code implemented in my website's theme file, and a Shortcode, I was able to display the View Count of one video. How does this code need to be changed, so it displays the View Counts of multiple videos of my choosing? Shortcode function: function youtube_view_count_shortcode($params) { $videoID = $params['id']; $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx"); $jsonData = json_decode($json); $views = $jsonData->items[0]->statistics->viewCount; return number_format($views); } add_shortcode('youtube_view_count', 'youtube_view_count_shortcode'); Shortcode: [[youtube_view_count id="UKuYgIBnqEA"]] I would really appreciate any suggestions. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/ Share on other sites More sharing options...
requinix Posted August 21, 2022 Share Posted August 21, 2022 Do you actually need these "shortcode" things for your site? Or do you just need to embed this number in a page somewhere? And how many videos are you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599659 Share on other sites More sharing options...
maxxd Posted August 21, 2022 Share Posted August 21, 2022 Is there a reason you're not just using the shortcode multiple times with different video IDs? The function just returns a number; you can add formatted text around the numbers. Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599661 Share on other sites More sharing options...
jacquesdegatineau Posted August 22, 2022 Author Share Posted August 22, 2022 (edited) @requinix It's 32 videos. That's right, using WordPress I need to use a Shortcode to display the number on the page. @maxxd Because like I said I want to show the sum of multiple View Counts combined as one number, that's why I was wondering if the function could be re-written. Edited August 22, 2022 by jacquesdegatineau Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599672 Share on other sites More sharing options...
requinix Posted August 22, 2022 Share Posted August 22, 2022 First thing you have to consider is that you don't want to hit the YouTube API 25 times every time you want these numbers. Wouldn't it be nice if you could get all of them at once? And you can: take that URL you have and add all the video IDs separated by commas. And you don't need a "id" parameter if you know exactly which videos you want. Then, since there will be multiple videos in the results, you'll need to total up all the views across all of them. That means a loop. $views = 0; foreach ($jsonData->items as $item) { $views += $item->statistics->viewCount; } Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599677 Share on other sites More sharing options...
gizmola Posted August 22, 2022 Share Posted August 22, 2022 If you look at the actual code, it's pretty simple. This is the api call from google: $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx"); Apparently, if the id = parameter includes a comma delimited list of videos, this api will still work (up to 50 videos). [[youtube_view_count id="UKuYgIBnqEA,xxxxxxxxx,xxxxxxxx"]] There is an entirely different analytics api you could use, which allows for statistics by "dimension" where your dimensions can be a channel, playlist, video or analytics "group". You maintain the groups through the youtube studio, and you can have up to 500 videos in a group. That would be easier to use and maintain long term. See https://support.google.com/youtube/answer/3529123?hl=en for more details on groups. Obviously you could get the statistics for the whole channel or add the videos to one or more playlists. This page lists some sample queries that could be used to get stats for a channel or a group: https://developers.google.com/youtube/analytics/sample-requests It looks to me like that is the way you want to go, but I don't have the time to look into it much further other than to suggest a look at: https://developers.google.com/youtube/analytics Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599678 Share on other sites More sharing options...
jacquesdegatineau Posted August 24, 2022 Author Share Posted August 24, 2022 Wow, you're geniuses, this works perfectly – thank you so much. @gizmola Thanks for your other suggestion as well, I'll have a look and experiment with that API soon, it sounds promising. Quote Link to comment https://forums.phpfreaks.com/topic/315217-need-some-help-with-customizing-this-php-code/#findComment-1599736 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.