This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/display-number-facebook-fans-php.md

31 lines
1.1 KiB
Markdown
Raw Normal View History

2015-03-16 21:18:03 +00:00
---
title: Display the Number of Facebook fans in PHP
2020-03-08 14:32:13 +00:00
date: 2011-03-15
2018-12-31 12:13:05 +00:00
excerpt: How to use PHP to display the number of fans of a Facebook page.
2015-04-13 11:42:09 +00:00
tags:
2015-06-14 02:27:41 +00:00
- php
2015-03-16 21:18:03 +00:00
---
Replace the \$page_id value with your Page ID number (unless you want to show
the number of fans for this site).You can find your Page ID by logging into your
Facebook account, going to 'Adverts and Pages', clicking 'Edit page', and
looking at the URL.
2015-03-16 21:18:03 +00:00
For example, mine is
<https://www.facebook.com/pages/edit/?id=143394365692197&sk=basic>.
I've also wrapped the output in a number_format() function so that it properly
formatted with commas etc - like where I've used it within the
[Gold Event listing](http://www.horseandcountry.tv/events/paid) on the Horse &
Country TV website.
2015-03-16 21:18:03 +00:00
2017-03-16 08:09:52 +00:00
```language-php
2015-03-16 21:18:03 +00:00
$page_id = "143394365692197";
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&amp;query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
print number_format($fans);
2017-03-16 08:09:52 +00:00
```
2015-03-16 21:18:03 +00:00
This code was originally found at
<http://wp-snippets.com/display-number-facebook-fans>.