我需要创建与我的网站和FB的集成,我对FB文档感到困惑(这是一个很长的词汇,而不是我的观点上的开发友好)和授权令牌。
我的“应用”应仅检查用户是否“喜欢”我的fb页面,并在此屏幕的基础上显示我网站上的一些内容。
我对以下几点感到困惑:
到目前为止我做了什么:
我正在使用2种方法:
1。图表API
<?php
$app_id = 'xxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxx';
$my_url = 'http://www.xxxxxxxxxxxxx.net/testfb.php';
$code = $_REQUEST["code"];
// auth user
if (empty($code)) {
$dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url);
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
// get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url)
. '&client_secret=' . $app_secret
. '&code=' . $code;
// response is of the format "access_token=AAAC..."
$access_token = substr(file_get_contents($token_url), 13);
$fql_query_url = 'https://graph.facebook.com/'
. 'fql?q=SELECT+uid+FROM+page_fan+WHERE+page_id=351144741645225+AND+uid=me()'
. '&access_token=' . $access_token;
$fql_query_obj = json_decode(file_get_contents($fql_query_url), true);
$data = array();
echo 'User Liked ?';
// display results of fql query
if (isset($fql_query_obj['data'])) {
$data = $fql_query_obj['data'];
}
if (isset($data[0])) {
echo 'YES';
} else {
echo 'NO';
}
2。 PHP SDK
<?php
require './lib/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
'cookie' => 'true'
));
//$access_token = $facebook->getAccessToken();
//$signed_request = $facebook->getSignedRequest();
// Get User ID
$user = $facebook->getUser();
var_dump($user);
//if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = 'yyyyyyyyyy' AND uid = me()"
));
var_dump($isFan);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
//} else {
// echo 'no logged in';
//}
答案 0 :(得分:0)
当用户首次加载您的应用时,它会通过POST请求发生。那时,Facebook发布了一个名为“signed_request”的字段。
您需要对其进行解码,然后您可以获取page.liked对象 - 它会告诉您用户是否喜欢该页面(感谢Facebook signed request get UID PHP):
$ signed_request = $ _REQUEST [“signed_request”];
list($ encoded_sig,$ payload)= explode('。',$ signed_request,2);
$ data = json_decode(base64_decode(strtr($ payload,' - _','+ /')),true);
echo $ data [“page.liked”];
似乎就是这样 - 不需要在facebook SDK或任何复杂的代码中使用。