Facebook PHP SDK -是用户的“喜欢”我的应用程序


Facebook PHP SDK - is user "liking" my app?

我有两点:-如何做:使用PHP SDK的Facebook检查用户喜欢我的应用程序?

我需要这个,因为客户需要它。

 $isLike = /* Code to check this */
if ($isLike){
//if user like my app
}else{
 //if not
 include 'generate.php';
}

我应该用"有效的"英语问这个问题吗?

这里有一个非常好的教程:http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/这应该可以让您快速了解SDK。然而,它不谈论点赞。要做点赞,您需要使用SDK (https://developers.facebook.com/docs/reference/api/user/#likes)调用图形API。该调用类似于$facebook->api("/$user/likes");:遍历喜欢列表,查看您的应用程序是否在列表中。

要检查用户是否喜欢你的应用你必须使用Facebook的PHP SDK并使用Facebook的like按钮,你的主文件如index.php:

//Include your PHP FB SDK :
require 'facebook.php';
//Check user loggin or not with your code 
try {   
$facebook = new Facebook ( array ('appId' => 'APP_ID', 'secret' => 'APP_SCRET', 'cookie' => true ) );
//Get current user ID
$user = $facebook->getUser ();
//Get your app info
$appinfo = $facebook->api ( "APP_ID" );
**//This link will be parse to FB Like button URL to like**
$linkToLike = $appinfo ['link'];
//Check if current user liked your app or not
$likeInfo = $facebook->api ( "$user/likes/APP_ID" );
if ($likeInfo['data'] == null){
    //User not yet liked your app, include page holding like button
include_once 'home.php';
}else{
echo 'You liked this application and now this is application info:'.'<br/>';
echo '<pre/>';
print_R ( $likeInfo );
exit ();
}
} catch (Exception $e) {
echo '<pre/>';
print_R ( $e );
exit ();
}

在home。php中你应该加上like button:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    var APP_ID = 'APP_ID';
FB.init({
  appId      : APP_ID, // App ID
  channelUrl : '//your_app_host_url/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});
FB.Event.subscribe('edge.create', function(response) {
//user just clicked "like" on your page
    alert('user just clicked Liked on your page');
    //Do something here :)
});
FB.Event.subscribe('edge.remove', function(response) {
//user just clicked "unlike" on your page
    alert('user just clicked "unlike" on your page');
    //Do something here :)
});
  };
</script>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=APP_ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<div class="fb-like" data-href="<?=$linkToLike?>" data-send="false"
data-layout="box_count" data-width="450" data-show-faces="false"></div>

希望有帮助!