So FQL depreciated after August 7,2016. What are you going to do if you have used FQL to get facebook like / share count in your webpage. Facebook wants us to use either SDK or API, But using sdk or api requires separate app for each domain which is not possible if you have service like, where user enters page url to check the like / share count. 

How do you get it?
The trick mentioned in this article is the best alternative to get facebook like / share count after fql depreciation. Using api or anyother methods not the right way, Then how do we going to get the count? After looking at facebook plugins i just found a simple trick to get the facebook like / share count directly.

Facebook javascript SDK
If you would have integrated javascript sdk to show "fb like" button in any of your website and you looked at where the iframe page gets the "html" to display the "like" button then you would got this idea. Yes, the solution is just to make a curl request similar to the one which iframe generates and parse the response to get the count. Look at the code below.
 
error_reporting(0);
function get_fblikes($url,$type='like'){
	$domain='webdevelopmentscripts.com';
	$protocol='http://';
	$orgin=$protocol.$domain.'/';

	$param=array('action'=>'like','app_id'=>'','channel'=>'http://staticxx.facebook.com/connect/xd_arbiter/r/lUqP5iIjiw6.js?version=42#cb=f178d9075b06a6a',
	'domain'=>$domain,'orgin'=>$orgin,'relation'=>'parent.parent','container_width'=>0,'href'=>$url,'layout'=>'button_count','locale'=>'en_GB','sdk'=>'joey',
	'share'=>false,'show_faces'=>true,'size'=>'large');

	if($type=='share'){
		$fb_url='https://www.facebook.com/v2.7/plugins/share_button.php?';
	}else{
		$fb_url='https://www.facebook.com/v2.6/plugins/like.php?';
	}

	$ch= curl_init();
	$options=array( 
		CURLOPT_URL => $fb_url.http_build_query($param),
		CURLOPT_FRESH_CONNECT => 1,
		CURLOPT_RETURNTRANSFER => 1, 
		CURLOPT_FORBID_REUSE =>1,
		CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01;Windows NT 5.0)",
		CURLOPT_SSL_VERIFYHOST=> 0,
		CURLOPT_SSL_VERIFYPEER=> 0, 
		CURLOPT_REFERER=>$orgin,
		CURLOPT_CONNECTTIMEOUT=> 10,
		CURLOPT_TIMEOUT=>40
  );
	        		 	 
	curl_setopt_array($ch,$options);
	$fb_html=curl_exec($ch);

	if(curl_errno($ch)>0){ #checks any curl error
		$fbc='';
	}else{ 
		#$fb_html=preg_replace('/s+/', '',$fb_html);
		#preg_match('/<spanid="u_0_3"[^>]*>(.*?)<\/span>/sm',$fb_html,$count);
		preg_match('/<span[^>]*id="u_0_3">(.*?)<\/span>/sm',$fb_html,$count);
		$fbc=$count[1];	
	}

	curl_close($ch);
	return $fbc;
}
FB like count:
Copy above code and simply call "get_fblikes" function with the url you want to see the like count, you're done. 
echo get_fblikes('http://webdevelopmentscripts.com');
FB share count:
To get facebook share count simply pass the second parameter as 'share',
echo get_fblikes('http://webdevelopmentscripts.com','share');
If you like to optimize this code further, just set a cookie to avoid the requests sent to facebook on each page load.

Example:
if(!isset($_COOKIE['fbcount'])){
	$fbc=get_fblikes('http://webdevelopmentscripts.com');
	echo $fbc;
	setcookie('fbcount',$fbc,0,'/');
  
}else{ $fbc=$_COOKIE['fbcount']; }

We have used cookies to avoid mass requests to facebook and i think this is the best way to get the facebook like / share count after fql depreciation. Drawback of using this method is, when facebook alters the html in which page like count set, this script will return empty response. But its out of our control, Periodic check and small adjustments to this script means we are on track again, Gud luck.

 


Comments (6)
  1. Image
    Arthur - Reply

    December 09, 2016

    Does not work: preg_match(): Unknown modifier p

    • Image
      WDS - Reply

      December 09, 2016

      Forgot to escape the backslash in the closing span tag... So we have to escape the slash in the closing span tag by adding backslash I have updated the code to reflect this, it will work now...

    • Image
      WDS - Reply

      December 09, 2016

      Also commented out replace spaces which cause another issue, So updated code in the example will work smoothly... Thanks for your notice about the issue.

Leave a Comment

loader Posting your comment...