Facebook API实时更新v2.2读取更新

时间:2014-11-18 16:33:50

标签: php facebook facebook-php-sdk real-time

我在php工作 我做了工作和sottoiscrizione

facebook api是v.2.2

但现在有问题 如何阅读我得到的Feed的更新?

代码是:

<?php

//file of program
require_once('LoginFb.php'); 
require_once('FbClass.php');
require_once('dbClass.php');
require_once('FacebookClass.php');

//receive a Real Time Update

$method = $_SERVER['REQUEST_METHOD'];                             

// In PHP, dots and spaces in query parameter names are converted to 
// underscores automatically. So we need to check "hub_mode" instead
//  of "hub.mode".                                                      
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&       
    $_GET['hub_verify_token'] == 'thisisaverifystring') {

    echo $_GET['hub_challenge'];    //print the code on the page that Facebook expects to read for confirmation

} else if ($method == 'POST') {                                   
  $updates = json_decode(file_get_contents("php://input"), true); 
  // Replace with your own code here to handle the update 
  // Note the request must complete within 15 seconds.
  // Otherwise Facebook server will consider it a timeout and 
  // resend the push notification again.

  $testo=json_decode($updates["entry"]);


  $var=fopen("nome_file.txt","a+");
  fwrite($var, "ciao");
  fwrite($var, $updates );

  fclose($var);



  error_log('updates = ' . print_r($updates, true));              
}

?>

在上面的文件&#34; $ update&#34;包含更新的Feed,但如何提取?

注意:订阅WORKS和更新已到达我的服务器。

请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

根据Facebook文档 [link]
请注意,实时更新仅指示特定字段已更改,它们不包括这些字段的值。它们应仅用于指示何时需要对该字段发出新的Graph API请求。

因此,您没有获得更新的数据,而是获得更新的领域名称。收到更新后,您应该提取更改的字段(我已在下面解释过)并向该字段发出新的Graph API请求。最后,您将获得更新的字段数据。

如何提取用户名和更改的字段? 你收到了这个:

{"entry":[{"id":"****","uid":"****","time":1332940650,"changed_fields":{"status"]}],"object":"user"}

其中“id”是我的pageId,“changed_fields”是更改字段的数组。 您可以按如下方式提取这些内容:

$entry = json_decode($updates["entry"]); <br>
$page = json_decode($entry["uid"]); <br>
$fields = json_decode($entry["changed_fields"]);

希望它有所帮助! :)

无功能 - &gt; respose包含一个数组数组,正确的代码是:

$json = json_decode($updates["entry"][0]["uid"], true);