如何轻松使用Facebook API实时更新功能? [英] How to easily use the Facebook API Realtime Updates feature?

查看:121
本文介绍了如何轻松使用Facebook API实时更新功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对Facebook发布另一个问题进行实时更新表示歉意。我已经阅读了许多现有的stackoverflow答案和有用的文章,这有助于我,但我仍然似乎无法弄清楚如何把所有东西放在一起。此外,这是我第一次使用Facebook和它的API,所以请放弃我。



所有我想要做的是当有用户时触发或者页面更新(无论是状态/评论/喜欢/等等)实时。



我从实时更新文档,并发现这两个博客文章:





从我的理解,注册对于实时更新,我需要:


  1. 创建一个WWW Facebook应用程序

  2. 指向Facebook应用程序到一个 callback_url

  3. 在Facebook调用时,在 callback_url 中添加一个php脚本来处理GET请求(验证)和POST请求。 / li>
  4. 使用Graph API注册回调( v2.3 / APP_ID / subscriptions

  5. 将Facebook登录按钮添加到页面(包括所需的范围/权限)并执行登录操作

从理论上讲,根据注册的对象和字段,Facebook应该POST到 callback_url



我想我已成功注册回调。这里大致输出我得到(用 MY_CB_URL 替换实际的URL):

  {
data:[
{
object:user,
callback_url:MY_CB_URL,
fields
status
],
active:true
},
{
object:page,
callback_url:MY_CB_URL,
fields:[
feed
],
active:true
}
]
$

回调PHP脚本如下所示:

 <?php 

define('VERIFY_TOKEN','vToken');

$ method = $ _SERVER ['REQUEST_METHOD'];

if($ method =='GET'&&_ $ _GET ['hub_mode'] =='subscribe'&& $ _GET ['hub_verify_token'] == VERIFY_TOKEN){

echo $ _GET ['hub_challenge'];

} else if($ method =='POST'){

$ out =;

尝试{

$ updates = json_decode(file_get_contents(php:// input),true);

$ out = print_r($ updates,true);

error_log('updates ='。$ out);
} catch(异常$ e){
error_log('捕获异常:'$ e-> getMessage());
$ out = $ e-> getMessage();
}

$ file ='./log.txt';
$ current = file_get_contents($ file);
$ current。= $ out;
file_put_contents($ file,$ current);

}

?>

我遇到的问题是,当我第一次设置这个时,我有一个POST请求,但没有那。我没有任何错误,API确认回调正确注册,所以我对我可能会丢失的东西无能为力。



我发现了这个答案,并打电话建议

  https://graph.facebook.com/PAGE_ID/tabs?app_id=APP_ID&access_token=PAGE_ACCESS_TOKEN 

并得到此回复:

 code> {
data:[
{
id:PAGE_ID / tabs / likes,
name:喜欢,
link:https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=likes,
is_permanent:true,
position:2,
is_non_connection_landing_tab:false
},
{
id:PAGE_ID / tabs / photos,
image_url:https:// fbcdn-photos -ca.akamaihd.net/hphotos-ak-xaf1/t39。 2080-0 / 851586_10151609549247733_1069686154_n.gif,
name:照片,
link:https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=photos ,
application:{
name:照片,
id:PHOTO_ID
},
is_permanent:false,
position:1,
is_non_connection_landing_tab:false
}
]
}

所以现在我进一步困惑了。

解决方案

发出POST请求 https://graph.facebook.com/PAGE_ID/tabs is 过期 - 您需要使用 / PAGE_ID / subscribed_apps 现在订阅一个页面的更新。



https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/


First off, apologies for posting yet another question on Facebook Realtime Updates. I have read many existing stackoverflow answers and useful articles which helped, but I still can't seem to figure out how to put everything together. Also, this is the first time I use Facebook and it's API, so please bare with me.

All I'm trying to do is get a trigger when there's a user or page updates (be it status/comment/like/etc.) in realtime.

I started with the Realtime Updates documentation and found these two blog posts handy:

From what I understood, to register for Realtime Updates, I need to:

  1. Create a WWW Facebook app
  2. Point the Facebook app to a callback_url
  3. Add a php script at the callback_url to handle a GET request (for verification) and POST requests when Facebook calls.
  4. Register the callback with the Graph API (v2.3/APP_ID/subscriptions)
  5. Add the Facebook Login button to the page (including the scope/permissions needed) and perform the login action

In theory, after this point, Facebook should POST to the callback_url based on the registered object and fields.

I think I've successfully registered the callback. Here roughly the output I get(with MY_CB_URL replacing the actual URL):

{
  "data": [
    {
      "object": "user", 
      "callback_url": "MY_CB_URL", 
      "fields": [
        "statuses"
      ], 
      "active": true
    }, 
    {
      "object": "page", 
      "callback_url": "MY_CB_URL", 
      "fields": [
        "feed"
      ], 
      "active": true
    }
  ]
}

The callback php script looks like so:

<?php

define('VERIFY_TOKEN', 'vToken');

$method = $_SERVER['REQUEST_METHOD'];

if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == VERIFY_TOKEN) {

    echo $_GET['hub_challenge'];

} else if ($method == 'POST') {

    $out = "";

    try {

        $updates = json_decode(file_get_contents("php://input"), true);

        $out = print_r($updates, true);

        error_log('updates = ' . $out);
    } catch (Exception $e) {
        error_log('Caught exception: '.$e->getMessage());
        $out = $e->getMessage();
    }

    $file = './log.txt';
    $current = file_get_contents($file);
    $current .= $out;
    file_put_contents($file, $current);

}

?>

The problem I have is I got a single POST request when I first set this up, but none after that. I don't get any errors and the API confirms the callback is correctly registered, so I am clueless on what I may be missing.

I've spotted this answer and make a call as suggested to

https://graph.facebook.com/PAGE_ID/tabs?app_id=APP_ID&access_token=PAGE_ACCESS_TOKEN

and got this response:

{
   "data": [
      {
         "id": "PAGE_ID/tabs/likes",
         "name": "Likes",
         "link": "https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=likes",
         "is_permanent": true,
         "position": 2,
         "is_non_connection_landing_tab": false
      },
      {
         "id": "PAGE_ID/tabs/photos",
         "image_url": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/t39.2080-0/851586_10151609549247733_1069686154_n.gif",
         "name": "Photos",
         "link": "https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=photos",
         "application": {
            "name": "Photos",
            "id": "PHOTO_ID"
         },
         "is_permanent": false,
         "position": 1,
         "is_non_connection_landing_tab": false
      }
   ]
}

So now I'm further confused.

解决方案

Making a POST request to https://graph.facebook.com/PAGE_ID/tabs is outdated – you need to use /PAGE_ID/subscribed_apps now to subscribe to updates from a page.

https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/

这篇关于如何轻松使用Facebook API实时更新功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆