Display a Facebook Page count without relying on the old public endpoint

Why the anonymous Facebook likes snippet is no longer a sound integration, and how to render an authorised, cached count safely.

The old example fetched a Page name anonymously and read a likes property. Do not use that as a current integration contract—or assume that a Page's likes, followers and an individual post's reactions are the same metric.

Separate obtaining the data from displaying it

For an API-backed counter, first establish access to the specific Page through Meta's developer setup. Confirm the available field, supported API version, token type and permissions for your app and Page. Those choices cannot be replaced by a universal token-free PHP snippet.

Keep credentials on the server. Fetch on a schedule, validate the response, and save a small projection containing only the public count and when it was checked. The website can then render that cached value without making a visitor wait for Facebook.

<?php
function renderPageCount(?int $count, string $pageUrl): string
{
    // This URL is supplied by the site's configuration, not a visitor.
    $link = '<a href="' . htmlspecialchars(
        $pageUrl, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'
    ) . '">Visit our Facebook Page</a>';

    if ($count === null || $count < 0) {
        return $link;
    }

    return '<p>' . number_format($count)
        . ' Page likes · ' . $link . '</p>';
}

// null means no verified, sufficiently recent value is available.
echo renderPageCount(null, 'https://www.facebook.com/YOUR_PAGE');

Use a fixed HTTPS Page URL. Pass a count only after your data-fetching job has validated it and applied your chosen freshness limit. If the retrieved metric is followers, change the label to followers; do not relabel it as likes.

A missing count is not zero

Expired access, provider errors and a genuinely empty audience are different states. Hide a stale or unavailable counter and keep the Page link working. Never put an access token in browser JavaScript, a public feed or an error message. A simple Page link is often more useful than maintaining a fragile vanity counter.

Use Meta's Pages API setup and Page reference for the access contract. The rendering example uses PHP's HTML escaping; it makes no Facebook request.

Original version25 June 2013

Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.

Here's a simple way to show a facebook page's current number of likes, it combines JSON and PHP, very simple! Can be used Facebook Profiles/Pages, as well as edited to work for general links and websites.

<?php
function fb_likes_count(){
$data = json_decode(file_get_contents("https://graph.facebook.com/guwii"));
echo $data->likes;}

fb_likes_count();
?>

At the time of writing that will output "91" (likes currently) straight away, just call the function fb_likes_count(); where you want it echo'd.
Example:

The number of likes on my Facebook page is: <?php fb_likes_count(); ?>

There is actually a lot more data available within that JSON object, here are the values returned that you can also hook onto if you wish:


{
   "about": "guwii - Web Design Agency in Cardiff.  https://guwii.com",
   "category": "Computers/technology",
   "is_published": true,
   "talking_about_count": 60,
   "username": "guwii",
   "website": "https://guwii.com",
   "were_here_count": 0,
   "id": "114477908707450",
   "name": "guwii",
   "link": "https://www.facebook.com/guwii",
   "likes": 91,
   "cover": {
      "cover_id": 199485470206693,
      "source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash3/s720x720/945532_199485470206693_1447682729_n.jpg",
      "offset_y": 0,
      "offset_x": 0
   }
}

Keep exploring

A couple more useful notes.