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

时间:2011-12-29 23:30:22

标签: php facebook

我有两件事:   - 怎么做:使用PHP SDK for Facebook检查用户是否喜欢我的应用程序?

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

 $isLike = /* Code to check this */

if ($isLike){

//if user like my app
}else{

 //if not

 include 'generate.php';
}

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

2 个答案:

答案 0 :(得分:1)

这里有一个非常好的教程:http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/这可以帮助您快速了解SDK。但是,它没有谈论喜欢。要做喜欢,您需要使用SDK调用图API(https://developers.facebook.com/docs/reference/api/user/#likes)。该调用看起来像$facebook->api("/$user/likes");通过喜欢的列表迭代,看看您的应用程序是否已列出。

答案 1 :(得分:0)

要检查用户是否喜欢您的应用,您必须使用来自Facebook的PHP SDK并使用FB 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中你应该有类似按钮的东西:

<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>

希望有所帮助!