Skip to content

Instantly share code, notes, and snippets.

@tiagocordeiro
Created October 21, 2022 14:18
Show Gist options
  • Save tiagocordeiro/1f48291de9a8d39034aee7db872a088c to your computer and use it in GitHub Desktop.
Save tiagocordeiro/1f48291de9a8d39034aee7db872a088c to your computer and use it in GitHub Desktop.
Funções para pegar dados de vídeos e de um canal no YouTube
<?php
function youtube_view_count_channel_shortcode($params)
{
$channelID = $params['id'];
$json = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" . $channelID . "&key=SUA-KEY");
$jsonData = json_decode($json);
$views = $jsonData->items[0]->statistics->viewCount;
return number_format($views);
}
add_shortcode('youtube_view_count_channel', 'youtube_view_count_channel_shortcode');
function youtube_subscriber_count_channel_shortcode($params)
{
$channelID = $params['id'];
$json = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" . $channelID . "&key=SUA-KEY");
$jsonData = json_decode($json);
$subscribers = $jsonData->items[0]->statistics->subscriberCount;
return number_format($subscribers);
}
add_shortcode('youtube_subscriber_count_channel', 'youtube_subscriber_count_channel_shortcode');
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=SUA-KEY");
$jsonData = json_decode($json);
$views = $jsonData->items[0]->statistics->viewCount;
return number_format($views);
}
add_shortcode('youtube_view_count', 'youtube_view_count_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment