---
title: How to Get YouTube Channel Subscriber Count without Using API
date: 2017-05-06T10:44:23+00:00
modified: 2019-01-21T09:43:49+00:00
image:: https://kaspars.net/wp-content/uploads/2017/05/youtube-subscribe-widget-html.png
permalink: https://kaspars.net/blog/youtube-subscriber-count
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
category:
  - Development
post_tag:
  - How to
  - PHP
  - Snippet
---

# How to Get YouTube Channel Subscriber Count without Using API

YouTube provides [these subscribe buttons](https://developers.google.com/youtube/youtube_subscribe_button) which [include the number of your channel subscribers](https://www.youtube.com/subscribe_embed?channelid=UCopSjr1a2QgRvm-yJRqcPpA) 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](https://www.youtube.com/account_advanced).

### Demo

Here is [the number of subscribers of my YouTube channel](https://kaspars.net/yt.php).

## Channels with 1k+ Subscribers

For YouTube channels with subscriber count above 1000 ([such as this](https://www.youtube.com/subscribe_embed?channelid=UCtinbF-Q-fVthA0qrFQTgXQ)) 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](https://developers.google.com/youtube/v3/) 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"
   }
  }
 ]
}
```