繁体   English   中英

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

[英]Facebook PHP SDK - is user “liking” my app?

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

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

 $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并使用FB like button来实现类似这样的操作,您的主文件如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>

希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM