Get Facebook share count

Hey,
I’m trying to get facebook share count for my blog posts.

I have this code:

$facebook_like_share_count = function ($url) {
	$api = file_get_contents('http://graph.facebook.com/?id=' . $url);
	$count = json_decode($api);
	return $count->id;
};

...

echo $facebook_like_share_count(esc_url(get_permalink()));

And I have this error: Notice: Undefined property: stdClass::$id in /public_html/wp-content/themes/my-theme/single.php on line 12 (which is return $count->id;)

It worked for a few days, I changed nothing. WTF?

No one knows how to display Facebook share count? :confused:

According to the PHP SDK, the call should look something like this.

/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
  $session,
  'GET',
  '/',
  array (
    'id' => 'http://www.imdb.com/title/tt2015381/',
  )
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */

What you are doing is using the HTTP API.

What does var_dump($count); return?

Scott

I added

require_once (ABSPATH . '/wp-content/themes/my-theme/facebook-sdk/autoload.php');

$fb = new Facebook\Facebook([
  'app_id' => 'my_app_id',
  'app_secret' => 'my_app_secret',
  'default_graph_version' => 'v2.2',
  ]);

$request = new Facebook\FacebookRequest(
	  $session,
	  'GET',
	  '/',
	  array (
	    'id' => 'http://www.imdb.com/title/tt2015381/',
	  )
	);
$response = $request->execute();
$graphObject = $response->getGraphObject();

and I get this errors:

Notice: Undefined variable: session in /home/domains/my_domain/public_html/wp-content/themes/my-theme/content-single.php on line 14

Warning: parse_url() expects parameter 1 to be string, array given in /home/domains/my_domain/public_html/wp-content/themes/my-theme/l/FacebookUrlManipulator.php on line 109

Warning: parse_url() expects parameter 1 to be string, array given in /home/domains/my_domain/public_html/wp-content/themes/my-theme/l/FacebookUrlManipulator.php on line 43

Fatal error: Call to undefined method Facebook\FacebookRequest::execute() in /home/domains/my_domain/public_html/wp-content/themes/my-theme/content-single.php on line 21

var_dump($count); return

object(stdClass)#2351 (3) { ["og_object"]=> object(stdClass)#2352 (6) { ["id"]=> string(15) "716518898469978" ["description"]=> string(503) "Hey, I'm trying to get facebook share count for my blog posts. I have this code: $facebook_like_share_count = function ($url) { $api = file_get_contents('http://graph.facebook.com/?id=' . $url); $count = json_decode($api); return $count->id; }; ... echo $facebook_like_share_count(esc_url(get_permalink())); And I have this error: Notice: Undefined property: stdClass::$id in /public_html/wp-content/themes/my-theme/single.php on line 12 (which is return $count->id;) It worked for a few ..." ["title"]=> string(24) "Get Facebook share count" ["type"]=> string(7) "website" ["updated_time"]=> string(24) "2015-07-13T20:15:10+0000" ["url"]=> string(64) "http://www.sitepoint.com/community/t/get-facebook-share-count/195541" } ["share"]=> object(stdClass)#2353 (2) { ["comment_count"]=> int(0) ["share_count"]=> int(0) } ["id"]=> string(64) "http://www.sitepoint.com/community/t/get-facebook-share-count/195541" }

Ok. Now you need to find out why your $session variable is missing/ not defined.

And from the $count dump your variable for the count should have been

$count->share->share_count

Which is 0.

Scott

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.