Facebook real-time updates [closed]
Asked Answered
F

0

9

I was trying to implement the FB Real-Time Updates. I guess I have managed to subscribe, but don't get any updates... Here is my code:

For getting App access token

function authAsApp() {
    $config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
    $fb = new Facebook($config);
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;


    $args = array('grant_type' => 'client_credentials',
            'client_id' => APP_ID,
            'client_secret' =>  APP_SECRET);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/oauth/access_token';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $returnValue = curl_exec($ch);
    curl_close($ch);

    $appAccessToken = substr($returnValue,  strpos($returnValue,"=")+1);

    var_dump($appAccessToken);

    return $appAccessToken;
}

For subscribtion

function subscribeTo($access_token) {
    $param = array('access_token' => $access_token,
            'object' => 'user',
            'fields' => 'name, feed, likes',
            'callback_url' => 'http://mysite.com/realTimeVerification.php',
            'verify_token' => VERIF_STR
    );
    $config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
    $fb = new Facebook($config);
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

    try {

    $subs = $fb->api('/'.APP_ID.'/subscriptions', 'POST', $param);
    var_dump($subs);
    } catch (\FacebookApiException $e) {
        echo $e->getCode()." ".$e->getType()." ".$e->getMessage()." ".$e->getFile()." ".$e->getLine()."\n";
    }
}

In the callback file named realTimeVerification.php I have

<?php
define('UPD_FILE', 'updates.log');
$method = $_SERVER['REQUEST_METHOD'];
global $log_file;

if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == 'BRComm') {
   echo $_GET['hub_challenge'];
   exit;
}
else if ($method == 'POST')
{
    $log_file=UPD_FILE;
    $updates = json_decode(file_get_contents("php://input"), true); 

    logToFile("updates =".print_r($updates));
}
function logToFile($message){
    global $log_file;

    $hdl = fopen($log_file, 'a') or die ("couldn't open log file");
    fwrite($hdl,$message."\n");
    fclose($hdl);
}
?>

After subscription I check it in Graph API Explorer and get:

{
  "data": [
    {
      "object": "user", 
      "callback_url": "http://mysite.com/realTimeVerification.php", 
      "fields": [
        "feed", 
        "name"
      ], 
      "active": true
    }
  ]
}

So it's subscribed. But when I do changes to my user that has authenticated the App, I don't get any changes in the log. What am I doing wrong?

Filler answered 16/2, 2012 at 15:53 Comment(3)
By the way, I guess the callback is not even executed when there is an update. I added logging at the top of callback, an no results.Filler
Have you tried subscribing for the real-time updates through the facebook developers site? It has test functionality which might help point out the problem.Diminuendo
actually this issue is resolved a long time ago. SO long that I don't remember what was the problem :)Filler

© 2022 - 2024 — McMap. All rights reserved.