Tuesday, April 2, 2013

get user realtime update for your app

First, you should go to your app setting page in facebook developer home page.

when you reach the app setting panel or dashboard,

choose  Settings-Realtime Updates

then in the dashboard, you can
1) in fields: input some the field you like, such as hometown_location
2) in callback: input your app url
    if your app url is http://myapp.com
    and your file to preocess the realtime update is  update.php
    then you should input: http://myapp.com/update.php
  
3) in verify token, you should input one string you like, e.g.: myfacebookapp123

save the changes.

Then you should prepare the update.php file on your server, you could copy the following code to your update.php file

<?php

  $verify_token = myfacebookapp123';
  if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['hub_mode'])
    && $_GET['hub_mode'] == 'subscribe' && isset($_GET['hub_verify_token'])
    && $_GET['hub_verify_token'] == $verify_token) 
 {
      echo $_GET['hub_challenge'];
   } else if ($_SERVER['REQUEST_METHOD'] == 'POST')
   {
    $post_body = file_get_contents('php://input');
    $obj = json_decode($post_body, true);
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'a+') or die("can't open file");
    $stringData = "Something updates, pls go to get it!\n";
    fwrite($fh, $stringData);
    fclose($fh);
  }
?>

Now you can go to facebook and modify one user's(who has authened your app before) hometown. after 1-2 minutes, your server could generate one testFile.txt with "Something updates, pls go to get it!" This post dose not analyze the content in  $obj . it is something like this:
  /*
  {
  "object": "user",
  "entry": [
    {
      "uid": 1335845740,
      "changed_fields": [
        "name",
        "picture"
      ],
      "time": 232323
    }, {
      "uid": 1234,
      "changed_fields": [
        "friends"
      ],
      "time": 232325
    }
  ]
}
  */

you can try to get them respectively use php. One thing we should pay attention to is that: through realtime update, we can only get the which filed has been updated, such as hometown. but we do not know what is the user's latest howntown unless we write codes to crawl that data.

No comments:

Post a Comment