Facebook Real for Feeds

时间:2012-11-26 12:09:32

标签: facebook facebook-graph-api facebook-php-sdk

我正在尝试使用Facebook Realtime Updates API ...我有以下代码:

<?php

define('VERIFY_TOKEN', '*');
$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') {

    $hostname = "*";
    $username = "*";
    $dbname = "*";
    $password = "*";

    mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname) or die(mysql_error());

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

    foreach ($object->entry as $update) {
        // For each entry in notification, insert a row of information into the table "FaceBook"
        $changed_fields = $update->changed_fields[0];
        $result = mysql_query("INSERT INTO FaceBook (uid, id, time, changed_fields) VALUES('$update->uid', '$update->id', '$update->time', '$changed_fields')")
                or die(mysql_error());
    }
    mysql_close();
}
?>

现在,当发生更新时,facebook正在发送POST请求,但是没有输入foreach循环...我试图检查$ object是否为空,但它返回2个元素(使用count)。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

也有一些问题。

每个对象都有许多条目。我相信每个条目都会有一些变化。所以“更改”也是一个类似条目的数组。

试试这个:

foreach($object->entry as $update){

    foreach($update->changes as $change){

        //access information about the change that occured on the page

    }
}