我正在寻找一种方法,以便在创建新帖子时从Facebook获得通知。 到目前为止,我发现的唯一解决方案是每隔几分钟检查一次。 facebook sdk不提供这样的方法吗?
由于
答案 0 :(得分:0)
使用Facebook Graph API订阅
<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$app_url = 'http://YOURAPPURL';
$fields = 'feed';
$verify_token = 'YOURVERIFYTOKEN';
// Fetching an App Token
$app_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
.$app_id.'&client_secret='.$app_secret
.'&grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $app_token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
parse_str($res, $token);
if (isset($token['access_token'])) {
// Let's register a callback
$params = array(
'object'
=> 'user',
'fields'
=> $fields,
'callback_url'
// This is the endpoint that will be called when
// a User updates the location field
=> $app_url . '/index.php?action=callback',
'verify_token'
=> $verify_token,
);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'
.$app_id.'/subscriptions?access_token='
.$token['access_token']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$res = curl_exec($ch);
if ($res && $res != 'null') {
print_r($res);
}
// Fetch list of all callbacks
curl_setopt($ch, CURLOPT_POST, 0);
$res = curl_exec($ch);
}
if ($res && $res != 'null') {
print_r($res);
}
curl_close($ch);
?>
一些更有帮助的链接:
https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/
https://developers.facebook.com/docs/graph-api/real-time-updates/