Search code examples
phpfacebookfacebook-graph-apireal-timefacebook-requests

Facebook API Real Time updates for Page/Feed no response


I have a problem with Facebook Real-time subscription from my facebook page.

When I POST a subscription, i get the message from facebook about it to my call back url. Code:

$session = new FacebookSession('<APP ACCESS TOKEN>');
$request = new FacebookRequest(
  $session,
  'POST',
  '/<APP ID>/subscriptions',
  array(
    'object' => 'page',
    'callback_url' => 'http://*************/facebook/callback.php',
    'fields' => 'conversation', // a try 'feed' to
    'verify_token' => '<VERIFY TOKEN>',
  )
);
$response = $request->execute();

BUT when someone add a post/conversation to my fb page, facebook don't send me any data.

callback.php code:

<?php

// Insert the path where you unpacked log4php
include('log4php/Logger.php');

// Tell log4php to use our configuration file.
Logger::configure('config.xml');

// Fetch a logger, it will inherit settings from the root logger
$log = Logger::getLogger('myLogger');

// Start logging
$log->warn($_REQUEST);   // Logged because WARN >= WARN

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\Authentication\AccessToken;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

//define('VERIFY_TOKEN', '*******');

$method = $_SERVER['REQUEST_METHOD'];





if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == '<VERIFY TOKEN>') {
    echo $_GET['hub_challenge'];
    $log->warn($_GET);
    $log->warn($_POST);
} else if ($method == 'POST') {
    $updates = json_decode(file_get_contents("php://input"), true);
    // Here you can do whatever you want with the JSON object that you receive from FaceBook.
    // Before you decide what to do with the notification object you might aswell just check if
    // you are actually getting one. You can do this by choosing to output the object to a textfile.
    // It can be done by simply adding the following line:
    // file_put_contents('/filepath/updates.txt',$updates, FILE_APPEND);

    $log->warn($updates);
    $log->warn($_GET);
    $log->warn($_POST);
    error_log('updates = ' . print_r($obj, true));
}

Thank you for your help !


Solution

  • I find answer with a big help from facebook developers forum friends :)

    My code to make subscription and callback code is good. But to get conversations or feed it is not enough. I have to add my app to my page as a subscribed_app using request in graph api: POST {page-id}/subscribed_apps

    the docs for this operation is on: https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/