脸书实时更新
我正在尝试实现 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
}
]
}
所以它已订阅。但是,当我对已验证应用程序的用户进行更改时,我在日志中没有收到任何更改。我做错了什么?
I was trying to implement the FB Real-Time Updates. I guess I have managed to subscribe, but don't get any updates... Here is my code:
For getting App access token
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;
}
For subscribtion
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";
}
}
In the callback file named realTimeVerification.php I have
<?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);
}
?>
After subscription I check it in Graph API Explorer and get:
{
"data": [
{
"object": "user",
"callback_url": "http://mysite.com/realTimeVerification.php",
"fields": [
"feed",
"name"
],
"active": true
}
]
}
So it's subscribed. But when I do changes to my user that has authenticated the App, I don't get any changes in the log. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论