我正在尝试实施FB实时更新。我想我已经设法订阅了,但没有得到任何更新...这是我的代码:
获取应用访问令牌
function authAsApp() {
$config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
$fb = new Facebook($config);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$args = array('grant_type' => 'client_credentials',
'client_id' => APP_ID,
'client_secret' => APP_SECRET);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$returnValue = curl_exec($ch);
curl_close($ch);
$appAccessToken = substr($returnValue, strpos($returnValue,"=")+1);
var_dump($appAccessToken);
return $appAccessToken;
}
订阅
function subscribeTo($access_token) {
$param = array('access_token' => $access_token,
'object' => 'user',
'fields' => 'name, feed, likes',
'callback_url' => 'http://mysite.com/realTimeVerification.php',
'verify_token' => VERIF_STR
);
$config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
$fb = new Facebook($config);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
try {
$subs = $fb->api('/'.APP_ID.'/subscriptions', 'POST', $param);
var_dump($subs);
} catch (\FacebookApiException $e) {
echo $e->getCode()." ".$e->getType()." ".$e->getMessage()." ".$e->getFile()." ".$e->getLine()."\n";
}
}
在名为realTimeVerification.php的回调文件中,我有
<?php
define('UPD_FILE', 'updates.log');
$method = $_SERVER['REQUEST_METHOD'];
global $log_file;
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == 'BRComm') {
echo $_GET['hub_challenge'];
exit;
}
else if ($method == 'POST')
{
$log_file=UPD_FILE;
$updates = json_decode(file_get_contents("php://input"), true);
logToFile("updates =".print_r($updates));
}
function logToFile($message){
global $log_file;
$hdl = fopen($log_file, 'a') or die ("couldn't open log file");
fwrite($hdl,$message."\n");
fclose($hdl);
}
?>
订阅后,我在Graph API Explorer中查看并获取:
{
"data": [
{
"object": "user",
"callback_url": "http://mysite.com/realTimeVerification.php",
"fields": [
"feed",
"name"
],
"active": true
}
]
}
所以它订阅了。但是,当我对已经对应用程序进行身份验证的用户进行更改时,我不会在日志中进行任何更改。我做错了什么?