Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@mustafakucuk

This comment has been minimized.

Copy link

@mustafakucuk mustafakucuk commented Nov 22, 2017

Thanks, for jQuery;

var url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=pewdiepie&fields=items/statistics/subscriberCount&key=_API_KEY_';

$.getJSON(url)
.done(function(data){
   alert(data.items[0].statistics.subscriberCount);
})
@MarcinMariuszMorawski

This comment has been minimized.

Copy link

@MarcinMariuszMorawski MarcinMariuszMorawski commented Apr 5, 2018

PHP > 5
`<?php
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);

$channel_id = " ";
$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, false, stream_context_create($arrContextOptions));
$api_response_decoded = json_decode($api_response, true);
echo $api_response_decoded['items'][0]['statistics']['subscriberCount'];
?>`

@TrungHieuVNTN

This comment has been minimized.

Copy link

@TrungHieuVNTN TrungHieuVNTN commented Oct 29, 2018

How to make realtime? Please

@sarp89

This comment has been minimized.

Copy link

@sarp89 sarp89 commented Feb 12, 2019

Dude the limit is ridiculous, I did it through a bash script with curl and inserted the counts into a database, because with client-side is impossible to NOT hit the limit. I think getting a bit more specific saves some of ur units, but it doesn't matter that much.

@ngocanhth

This comment has been minimized.

Copy link

@ngocanhth ngocanhth commented Jun 15, 2019

I want to check a channel A has registered channel B or not? Similarly, check for channel A with video view or like video B how do I do it ? Please

@jaykepeters

This comment has been minimized.

Copy link

@jaykepeters jaykepeters commented Jun 15, 2019

@ostermine

This comment has been minimized.

Copy link

@ostermine ostermine commented Aug 27, 2019

<?php
if ($_GET["c"]) {
	$channel_id = $_GET["c"];
	$api_key = "YOUR 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);
	$z = $api_response_decoded['items'][0]['statistics']['subscriberCount'];
	if (empty($z)) {
		echo 'Bad YouTube channel id!';
	} else {
		echo $z;
	}
} else {
	echo "Bad request! use ?c=channel id";
}
?>
@WinsDominoes

This comment has been minimized.

Copy link

@WinsDominoes WinsDominoes commented Oct 8, 2019

Hello, how could I make it real-time? Thanks!

@jaykepeters

This comment has been minimized.

Copy link

@jaykepeters jaykepeters commented Oct 8, 2019

@WinsDominoes, as you probably know, that is not possible with PHP, but just Google realtime API requests/real-time javascript updates, etc, and you will figure it out. Or put it on a timer, say every second, to update the stats...

@elfq

This comment has been minimized.

Copy link

@elfq elfq commented Nov 3, 2019

I'm looking to make a subscriber count like social blade, can I do that with this?

@MarsKittenPro

This comment has been minimized.

Copy link

@MarsKittenPro MarsKittenPro commented Nov 8, 2019

Mine doesn't work? i have it in PHP 7 but it doesn't show up with anything, can i please have some help?

@denvermatterhorn

This comment has been minimized.

Copy link

@denvermatterhorn denvermatterhorn commented Jan 25, 2020

It's possible to make it real time by creating a login to your account and making an POST api call to https://www.youtube.com/youtubei/v1/creator/get_channel_dashboard

@bitmammoth

This comment has been minimized.

Copy link

@bitmammoth bitmammoth commented Sep 15, 2020

Try this with reactphp for async calls

@PusparajPoudyal

This comment has been minimized.

Copy link

@PusparajPoudyal PusparajPoudyal commented Sep 17, 2020

I need instagram follower count api. Previous api not working in 2020. Can anyone help me?

@mianusman9808

This comment has been minimized.

Copy link

@mianusman9808 mianusman9808 commented Jan 12, 2021

Undefined index: items i am getting error like this how can I resolve this error i laravel
my code is
public function youtube()
{
$channel_id = '';
$api_key = '';
$api_response = $this->curl_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'];

}
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;
}
@nonsintetic

This comment has been minimized.

Copy link
Owner Author

@nonsintetic nonsintetic commented Jan 12, 2021

mianusman9808 - add a var_dump($api_response_decoded); before the echo, see what you're getting back, maybe it's some API error or the format has changed. Haven't checked this in a while

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment