3 Ways of Getting Shares for URL using Facebook 3.0 API

No matter what way you choose, in any case you should make a little preparation — create a Facebook App and get Access Token from it (App ID|App Secret).

Step 1. Creating Facebook App

1. Go to Facebook Developers page and click Add a New App link.

Facebook Applications List. Ann a new app.

2. After clicking that link a popup will appear. Input any App Name, your Contact Email and select Category.

3. Get App ID and App Secret on this page:

How to get Facebook Application ID and Secret

4. The last step is to make your facebook application public.

How to make Facebook Application Public

So, that’s it, in all examples in this post our Access Token will look like this: Application ID|App Secret.

Step 2. Get Shares Count. Ready-to-use Examples.

WordPress, cURL and jQuery examples are below.

WordPress HTTP API example

If you are not familiar with WordPress, insert the following code to the functions.php file of your active theme.

 * Display number of shares using WordPress HTTP API
 * @param integer $post_id We want to get number of shares of the post with this ID
function wp_get_shares( $post_id ) {
	$cache_key = 'misha_share' . $post_id;
	$access_token = 'APP_ID|APP_SECRET';
	$count = get_transient( $cache_key ); // try to get value from WordPress cache

	// if no value in the cache
	if ( $count === false ) {
		$response = wp_remote_get( add_query_arg( array( 
			'id' => urlencode( get_permalink( $post_id ) ),
			'access_token' => $access_token,
			'fields' => 'engagement'
		), 'https://graph.facebook.com/v3.0/' ) );
		$body = json_decode( $response['body'] );
		$count = intval( $body->engagement->share_count ); 

		set_transient( $cache_key, $count, 3600 ); // store value in cache for a 1 hour
	return $count;

cURL example


 * Display number of shares using PHP cURL library
 * @param string $url ID We want to get number of shares of this URL
function curl_get_shares( $url ){
	$access_token = 'APP ID|APP SECRET';
	$api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
	$fb_connect = curl_init(); // initializing
	curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
	curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
	curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );
	$json_return = curl_exec( $fb_connect ); // connect and get json data
	curl_close( $fb_connect ); // close connection
	$body = json_decode( $json_return );
	return intval( $body->engagement->share_count );


echo curl_get_shares( 'https://rudrastyh.com/api/get-facebook-shares.html' );

jQuery example with live demo

Well, I hope you know what is jQuery and how to work with it. Here is the code and there is a live demo below.

var token = 'APP ID|APP SECRET', // learn how to obtain it above
url = 'YOUR URL';

	url: 'https://graph.facebook.com/v3.0/',
	dataType: 'jsonp',
	type: 'GET',
	data: {fields: 'engagement', access_token: token, id: url},
	success: function(data){
	error: function(data){
		console.log(data); // send the error notifications to console
[codepen_embed height=”265″ theme_id=”light” slug_hash=”EpaNzm” default_tab=”js,result” user=”rudrastyh”]See the Pen EpaNzm by Misha.[/codepen_embed]
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X