Don't display facebook like button for those who already liked it

筅森魡賤 提交于 2019-12-03 08:32:35

Implement the JavaScript SDK

During the initialization of the JS SDK, make sure to set xfbml to false, so that the plugin will not be rendered automatically. You'll have to request the user_likes permission which means that you are going to have to associate your site with a Facebook application. This is so that Facebook can keep track of who is requesting what data. You can use the Fb.login() method to request permissions and authenticate your users.

Once you have the JavaSctipt SDK you can simply make a call to the API via

  • Graph
    FB.api('me/likes/PAGE_ID')

  • FQL
    FB.api("fql?q='SELECT+uid+FROM+page_fan+WHERE+uid=me()+AND+page_id=PAGE_ID")

If those return data you know not to display the like plugin. However, if the user has not liked the page yet, those calls will return empty results and then you can call the FB.XFBML.parse() method to render the like plugin.

You need to make some calls to the Graph API,
After you get the Page ID with FB.api('http://your-url.com');
you can check if current user like's your page with an FQL query:

'SELECT uid FROM page_fan WHERE uid=me() AND page_id=YOUR-PAGE-ID'
borisdiakur

If your like button is on a canvas page or a page tab you can implement a fan gate by reading the signed_request. There is a ton of tutorials and hints on how to do it also here on stackoverflow for different server side languages. For example this answer here: https://stackoverflow.com/a/7671496/601466
Just search for "fan gate" and "signed request".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!