Skip to content

Instantly share code, notes, and snippets.

@komputronika
Forked from nonsintetic/readme.md
Created January 9, 2019 06: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 komputronika/811c75f0c66b0fe62577027c03b64cd4 to your computer and use it in GitHub Desktop.
Save komputronika/811c75f0c66b0fe62577027c03b64cd4 to your computer and use it in GitHub Desktop.
PHP - Get YouTube subscriber count with Youtube API v3

How to:

Here's a 'simple' way to get the YouTube subscriber number from Google's Youtube API v3:

  1. Go to https://console.developers.google.com/apis/library

  2. Log in with your Google account.

  3. Next to the logo click on 'Project' and 'Create project'. Name it whatever you want and click on 'Create'.

  4. Wait until the project is created, the page will switch to it by itself, it will take a couple of seconds up to a minute. Once it's done it will be selected next to the logo.

  5. Once it's created and selected, click on 'Credentials' from the menu on the left.

  6. Click on 'Create Credentials' and choose 'API Key'. You can restrict it to specific IPs, or types of requests (website, android, ios etc.) if you want, it's safer that way.

  7. Copy your API KEY, you will need this in the script.

  8. Make a new PHP file on your web server and use the following code, replacing YOUR_CHANNEL_ID and API_KEY with your channel's ID (it's the code at the end of your channel's URL) and the API Key you got in step 7.

<?php
$channel_id = "YOUR_CHANNEL_ID";
$api_key = "API_KEY";
$api_response = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.$channel_id.'&fields=items/statistics/subscriberCount&key='.$api_key);
$api_response_decoded = json_decode($api_response, true);
echo $api_response_decoded['items'][0]['statistics']['subscriberCount'];
  1. When you navigate to your script it will just spit out the subscriber count.

Hope it helps!

Troubleshooting:

If your server doesn't handle file_get_contents ok (some don't), you can try the CURL version of it. Paste this following function at the end of your file and replace file_get_contents in the code with curl_get_contents.

function curl_get_contents($url,$useragent='cURL',$headers=false, $follow_redirects=false,$debug=false) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($headers==true){
		curl_setopt($ch, CURLOPT_HEADER,1);
	}
	if ($headers=='headers only') {
		curl_setopt($ch, CURLOPT_NOBODY ,1);
	}
		if ($follow_redirects==true) {
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	if ($debug==true) {
		$result['contents']=curl_exec($ch);
		$result['info']=curl_getinfo($ch);
	} else {
		$result=curl_exec($ch);
	}
	curl_close($ch);
	return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment