How to Get YouTube Channel Subscriber Count without Using API

YouTube provides these subscribe buttons which include the number of your channel subscribers which can be extracted using a simple regular expression.

Extract the Subscribe Count in PHP

<?php

// Change channelid value to match your YouTube channel ID
$url = 'https://www.youtube.com/subscribe_embed?channelid=UCopSjr1a2QgRvm-yJRqcPpA';

// Fetch the Subscribe button HTML
$button_html = file_get_contents( $url );

// Extract the subscriber count
$found_subscribers = preg_match( '/="0">(\d+)</i', $button_html, $matches );

if ( $found_subscribers && isset( $matches[1] ) ) {
    echo intval( $matches[1] );
}

The Channel ID can be found on your YouTube profile page.

Demo

Here is the number of subscribers of my YouTube channel.

Channels with 1k+ Subscribers

For YouTube channels with subscriber count above 1000 (such as this) it will display a “human” readable string “10M” instead of the actual number. In those cases you’ll need to use the official YouTube API like so:

<?php

$api_url = 'https://content.googleapis.com/youtube/v3/channels';

$query = [
    'id' => 'UCopSjr1a2QgRvm-yJRqcPpA', // Channel ID.
    'key' => 'YOURAPISECRECTFROMTHEGOOGLECONSOLE', // Your API Key.
    'part' => 'statistics',
];

$request_url = sprintf(
    '%s?%s',
    $api_url,
    http_build_query( $query )
);

$response_json = json_decode( file_get_contents( $request_url ) );

echo $response_json->items[0]->statistics->subscriberCount;

and we’re extracting $response_json->items[0]->statistics->subscriberCount from the $response_json which looks like this:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/k-b8cAz8jrfCs9f_OWym7RX4Nkg\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/DCgZF-rKkOqC-OcyBd5OU5kXbUI\"",
   "id": "UCopSjr1a2QgRvm-yJRqcPpA",
   "statistics": {
    "viewCount": "156320",
    "commentCount": "0",
    "subscriberCount": "873",
    "hiddenSubscriberCount": false,
    "videoCount": "13"
   }
  }
 ]
}

Use Contact Form 7 to collect business leads and enquiries? I created Storage for Contact Form 7 plugin which stores them safely in WordPress database.

Get it now for only $19 →

2 Comments

  1. Sangeetha says:

    The above code is not working for the youtube channels, whose subscriber count has letters. E.g.100K. Can you please post the code for handling that?

Leave a Reply to Kaspars Cancel reply