创建新帖子时收到通知

时间:2014-02-03 22:00:41

标签: php facebook facebook-graph-api

我正在寻找一种方法,以便在创建新帖子时从Facebook获得通知。 到目前为止,我发现的唯一解决方案是每隔几分钟检查一次。 facebook sdk不提供这样的方法吗?

由于

1 个答案:

答案 0 :(得分:0)

使用Facebook Graph 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);

?>

一些更有帮助的链接:

https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/

https://developers.facebook.com/docs/graph-api/real-time-updates/