Search code examples
phpfacebookfacebook-graph-apifacebook-fql

Checking if someone has shared a certain link on Facebook


For my new website I wanted to use a Facebook-button/icon I had found, different than the standard like button. Furthermore I prefer sharing above liking URLs on Facebook. Therefore I made the icon open a popup onclick showing http://facebook.com/sharer/sharer.php with the right parameters. Using the Facebook graph API I succeeded to get the number of shares of my URL and show it next to the button using the following PHP-script (retrieving) and JavaScript (showing):

<?php
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=SELECT+total_count+FROM+link_stat+WHERE+url="URL_GOES_HERE"';
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_num = substr($fql_query_result, 24);
$fql_query_num = substr($fql_query_num, 0, -3);                  
?>

<script type="text/javascript">
function showShares(){
document.getElementById('fb_count').innerHTML = <?php echo $fql_query_num;?>;
}
</script>

(The original file also contains a similar script for Twitter, which I've left out here to keep things clear.)

A function I'd like to add now, is that when people have shared the link, the corresponding button turns grey/inactive onload of the page. I expected this could easily be done using a FQL query and a JavaScript, but I couldn't find it on http://developers.facebook.com yet. So I'm wondering whether someone here knows how to do this (if there is any way at all...).

Thanks in advance!


Solution

  • http://facebook.com/sharer/sharer.php is being deprecated. I'd suggest using the Graph API or one of the new Plugins Facebook has developed to take its place. Once you start implementing the new stuff, your questions might change. FWIW, I don't think there's a way to tell if a specific user has shared a link (unless it's a like for a Facebook page).