Last active
June 27, 2017 14:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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