我的搜索结果为空,因为当你搜索"Facebook"answers"赞"时,我会得到各种其他结果。
我有一个应用程序,它只出现在我公司的Facebook页面上。在该应用程序中,我需要了解用户是否喜欢该公司的页面。如果没有,我会展示一件事,如果是,我会显示另一件事。如何使用FacebookPHPSDKv.3.1.1来做到这一点?
可以这样做:
<?php
require('facebook.php');
$config = array(
'appId' => 'your facebook app id',
'secret' => 'your facebook app secret code',
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if (isset($user_id)) {
try {
$likes = $facebook->api('/me/likes/your_facebook_page_id_here', 'GET');
if (!empty($likes['data'])) // if user has liked the page then $likes['data'] wont be empty otherwise it will be empty
{
echo 'Thank you for liking our fan page!';
}
else {
//show something else
}
} catch (FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo '<a href="' . $login_url . '">Please click here to login into your Facebook account.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo '<a href="' . $login_url . '">Please lick here to login into your Facebook account</a>';
}
?>
用户将单击"请单击此处登录您的Facebook帐户"文本,该文本将重定向到Facebook应用程序权限页面,一旦用户允许访问您的应用程序,代码将获取用户的数据,并在用户不喜欢您的粉丝页面时显示您想要的内容。
您可以使用FQL来完成此操作。您还需要确保您拥有user_likes权限集。
我从一个现在离线的旧应用程序中提取了这个例子,它可能需要根据Facebook在上一轮更新中的变化进行更改。最近我一直在使用javascript,并订阅了edge.create事件。。。。只需将page_id替换为页面的id,并尝试
$checkIfUserLikePage = $facebook->api(array(
"method" => "fql.query",
"query" => "select uid from page_fan where uid=me() and page_id=1234567"
));
$checkIfUserLikePage = sizeof($checkIfUserLikePage) == 1 ? true : false;
这应该对您有用!我必须做很多这样类型的页面,所以我创建了一种非常简单的方式来创建类似门控页面。https://github.com/DrewDahlman/FBVersion
享受吧!
$signed_request = $_REQUEST['signed_request'];
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
$this->assign('verify', $signed_request->page->liked);
}
else {
echo "Please click on the Like button to view our Coupons!";
jexit();
}
}
我希望这将帮助您:)
老话题,但我想参与进来,因为我最近学到了很多关于如何做到这一点的知识,并不得不将其付诸实践。
请查看此链接:http://nickring.com/blog/2012/11/facebook-fan-gate-using-php/.正如其他一些响应所示,它使用signed_request
变量,但它显示了它如何不需要通过$_REQUEST
请求signed_request
变量。
需要记住的一件主要事情是,只有当访问signed_request
的PHP脚本在Facebook中运行时,signed_request
才可用如果您在试图使用Facebook API的脚本中在Facebook外部运行此脚本,它将返回一个空数组。
下面是一个示例-当您转到该地址时,将运行以下脚本:https://www.facebook.com/yourFacebookPage/app_xxxxxxxxxxxxxxx其中"xxxxxxxxxxxxxxx"是应用程序ID。
// Check if the user has liked us on Facebook, require the Facebook SDK
require_once('linked/facebook/facebook.php');
// Setup the Config
$config = array(
'appId' => 'xxxxxxxxxxxxxxxxx',
'secret' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
'cookie' => true
);
// Create a Facebook SDK instance
$facebook = new Facebook($config);
// Get the signed_request variable that we so desparately need
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
// Make sure that it worked
if (!empty($signed_request))
{
// Get the signed_request information
if ($like_status == 1)
{
// Wo0t! Show the FB fan only page stuff here
}
else
{
// Show the 'Please like us page'
$page = file_get_contents('pages/facebookLikeUs.html');
// Finish the page
echo $page;
exit();
} // End if ($like_status == 1) ELSE Clause and IF
} // End if (!empty($signed_request)) IF Clause
else
{
// Damn, it didn't work. Show an error
}
上述脚本是从"Canvas URL"中设置的URL调用的脚本;Facebook上的应用程序";部分。CCD_ 6页面只是要求他们点击";喜欢";继续。如果我希望他们被重定向回一个需要Facebook的网站,就像我简单地用这样的东西替换// Wo0t!
部分:
// Wo0t! They're all set, establish some cookies, get a cookie, and redirect back to the PHD program site
setcookie('fbls', $signed_request['page']['id'] . '-' . $signed_request['user_id'], time() + 300);
$redirectURL = "http://www.myurl.com/theScriptIWantToReceiveTheUserFromFB.php";
// Since Facebook really wants to keep us in the page, we need to create a page that will automatically break out of FB
$page = file_get_contents('pages/facebookRedirectBack.html');
// Replace some stuff
$page = str_replace('$redirectURL', $redirectURL, $page);
// Output the page
echo $page;
exit();
facebookRedirectBack.html
页面为:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
window.top.location.href = '$redirectURL';
</script>
</body>
</html>
如果你想在这方面有更多的安全性,你可以让重定向PHP脚本编写一些cookie,并将它们附加到URL,这样接收脚本必须比较cookie和URL参数,然后在读取cookie后删除它们。
希望这能有所帮助,因为我个人还没有找到任何关于这个主题的一致信息。