php – 在GRAPH API中检查页面的用户风扇的方法是什么?

前端之家收集整理的这篇文章主要介绍了php – 在GRAPH API中检查页面的用户风扇的方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在图api中,Pages.isFan方法不起作用,用图检查用户风扇的方法是什么?

谢谢.

更新2:
要检查当前用户是否在Facebook页面的粉丝登陆您的标签,请检查此 answer.

更新:
您可以使用喜欢的连接来检查用户是否是页面的粉丝:

  1. https://graph.facebook.com/me/likes/PAGE_ID
  2. &access_token=ACCESS_TOKEN

这将返回一个空数据数组:

  1. Array
  2. (
  3. [data] => Array
  4. (
  5. )
  6.  
  7. )

或者如果风扇:

  1. Array
  2. (
  3. [data] => Array
  4. (
  5. [0] => Array
  6. (
  7. [name] => Real Madrid C.F.
  8. [category] => Professional sports team
  9. [id] => 19034719952
  10. [created_time] => 2011-05-03T20:53:26+0000
  11. )
  12.  
  13. )
  14.  
  15. )

所以这是我们如何检查使用PHP-SDK:

  1. <?PHP
  2. require '../src/facebook.PHP';
  3.  
  4. // Create our Application instance (replace this with your appId and secret).
  5. $facebook = new Facebook(array(
  6. 'appId' => 'APP_ID','secret' => 'APP_SECRET',));
  7.  
  8. $user = $facebook->getUser();
  9.  
  10. if ($user) {
  11. try {
  12. $likes = $facebook->api("/me/likes/PAGE_ID");
  13. if( !empty($likes['data']) )
  14. echo "I like!";
  15. else
  16. echo "not a fan!";
  17. } catch (FacebookApiException $e) {
  18. error_log($e);
  19. $user = null;
  20. }
  21. }
  22.  
  23. if ($user) {
  24. $logoutUrl = $facebook->getlogoutUrl();
  25. } else {
  26. $loginUrl = $facebook->getLoginUrl(array(
  27. 'scope' => 'user_likes'
  28. ));
  29. }
  30.  
  31. // rest of code here
  32. ?>

使用JS-SDK的相似脚本:

  1. FB.api('/me/likes/PAGE_ID',function(response) {
  2. if( response.data ) {
  3. if( !isEmpty(response.data) )
  4. alert('You are a fan!');
  5. else
  6. alert('Not a fan!');
  7. } else {
  8. alert('ERROR!');
  9. }
  10. });
  11.  
  12. // function to check for an empty object
  13. function isEmpty(obj) {
  14. for(var prop in obj) {
  15. if(obj.hasOwnProperty(prop))
  16. return false;
  17. }
  18.  
  19. return true;
  20. }

代码取自我的tutorial.

虽然pages.isFan仍然适用于我,您可以使用新的PHP-SDK的FQL page_fan表:

  1. $result = $facebook->api(array(
  2. "method" => "fql.query","query" => "SELECT uid FROM page_fan WHERE uid=$user_id AND page_id=$page_id"
  3. ));
  4. if(!empty($result)) // array is not empty,so the user is a fan!
  5. echo "$user_id is a fan!";

从文档:

To read the page_fan table you need:

  • any valid access_token if it is public (visible to anyone on Facebook).
  • user_likes permissions if querying the current user.
  • friends_likes permissions if querying a user’s friend.

猜你在找的PHP相关文章