Check if user liked page | PHP | Facebook


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $session = $facebook->getSession();
  3.  
  4. $user = $facebook->getUser();
  5. $page = 'PAGEID';
  6.  
  7. // Session based API call.
  8. if ($session) {
  9. try {
  10. $likeID = $facebook->api(array( 'method' => 'fql.query', 'query' =>
  11. 'SELECT target_id FROM connection WHERE source_id =' . $user . 'AND target_id =' .$page ));
  12.  
  13. if ( empty($likeID) ) {
  14. // user has NOT Liked the page
  15. echo "Like my page!";
  16.  
  17. } else {
  18.  
  19. // user HAS Liked the page
  20. echo "Thank you for liking my page";
  21.  
  22. }
  23. } catch (FacebookApiException $e) {
  24. error_log($e);
  25. }
  26. }
  27. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.