[英]Get notified when new post created
I looking for a way to get notified from facebook whenever a new post has been created. 我正在寻找一种方法,以便在创建新帖子时从Facebook得到通知。 So far the only solution I've found is cron job to check every few minutes.
到目前为止,我发现的唯一解决方案是cron作业,每隔几分钟检查一次。 Don't the facebook sdk provide such method?
Facebook SDK不提供这种方法吗?
Thanks 谢谢
Use Facebook Graph API Subscriptions 使用Facebook Graph API订阅
https://developers.facebook.com/blog/post/2012/01/31/how-to--subscribing-to-data-changes-using-the-real-time-updates-api/ https://developers.facebook.com/blog/post/2012/01/31/how-to--subscribing-to-data-changes-using-the-real-time-updates-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);
?>
Some more helpful links: 一些更有用的链接:
https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/ https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/
https://developers.facebook.com/docs/graph-api/real-time-updates/ https://developers.facebook.com/docs/graph-api/real-time-updates/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.