Skip to content

Instantly share code, notes, and snippets.

@element121
Last active June 27, 2017 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save element121/aaa195e3aaaf6600d01144160d2a2279 to your computer and use it in GitHub Desktop.
Save element121/aaa195e3aaaf6600d01144160d2a2279 to your computer and use it in GitHub Desktop.
PHP Code to use YouTube's API to get statisticts on one or more YouTube Channels by their name.
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
// Youtube API to get stats for a channel
// Example call, but you will need an API key from Google to use the API
// https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=kontor&key={YOUR_API_KEY}
// Returns:
/*
{
"kind": "youtube#channelListResponse",
"etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/wUS5SuWuhn9EVxWUX1pQqQWrG9A\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/4QonHZaxbXgUV0ZP-JMr-gipk4Y\"",
"id": "UCb3tJ5NKw7mDxyaQ73mwbRg",
"statistics": {
"viewCount": "2672718210",
"commentCount": "9581",
"subscriberCount": "2928060",
"hiddenSubscriberCount": false,
"videoCount": "1452"
}
}
]
}
*/
// Comma seperated list of YouTube channel names to get the stats for
//$screen_names = "chevrolet,Chrysler,dodge,Acura,ford,BMWUSA,Honda,Hyundai,infiniti,kia,JaguarUSA,lexus,Lincoln,Mazda,mercedesbenzusa,Mitsubishi,Buick,nissancanada,nissanusa,porsche,subaruofamerica,toyota,vw,VolvocarsUS,SuzukiCycles,cadillac,audi,gmc,jeep,landroverusa";
$screen_names = "IngramMicroUK,tdukmedia,HP,Cisco,Acer,Toshiba,SONYUK,samsung,microsoft,MotorolaSolutions,htcgb,apple";
// Explode the list into an array
$screen_names_array = explode(",", $screen_names);
// Iterate through the array to get the stats
foreach ($screen_names_array as $screen_name) {
// Call the API to get the data
$data = (json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername='. $screen_name .'&key={YOUR_API_KEY}')));
//var_dump($data->items[0]->statistics->subscriberCount);
// Check data is returned and output it
if (isset($data))
{
// Some channels hide the subscriber count
if (!$data->items[0]->statistics->hiddenSubscriberCount)
echo "<br><a href='https://www.youtube.com/user/". $screen_name ."' target='_blank'>". $screen_name ."</a> Subscriber Count: ". number_format($data->items[0]->statistics->subscriberCount);
else
echo "<br><a href='https://www.youtube.com/user/". $screen_name ."' target='_blank'>". $screen_name ."</a> Subscriber Count: Count of subscribers private";
echo "<br><a href='https://www.youtube.com/user/". $screen_name ."' target='_blank'>". $screen_name ."</a> View Count: ". number_format($data->items[0]->statistics->viewCount);
echo "<br><a href='https://www.youtube.com/user/". $screen_name ."' target='_blank'>". $screen_name ."</a> Comment Count: ". number_format($data->items[0]->statistics->commentCount);
echo "<br><a href='https://www.youtube.com/user/". $screen_name ."' target='_blank'>". $screen_name ."</a> Video Count: ". number_format($data->items[0]->statistics->videoCount);
echo "<br><br>";
}
}
// End of youtube_channel_stats.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment