0

I was working in php I did the work and sottoiscrizione

facebook api is v.2.2

but now there is a problem how do I read the updates of the feeds I get ?

The code is :

<?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));              
}

?>

In the above file "$update" contains an updated feed, but how to extract?

Note: subscription WORKS and updates arrived on my server.

Help me please :)

abhi
  • 1,412
  • 19
  • 25
Bonni
  • 41
  • 2
  • 10

1 Answers1

0

According to the Facebook documentation [link]:
Note that real-time updates only indicate that a particular field has changed, they do not include the value of those fields. They should be used only to indicate when a new Graph API request to that field needs to be made.

So, you don't get the updated data instead you get the updated feild name. On receiving an update, you should extract the changed field (I have explained this below) and make a new Graph API request to that field. Finally, you will get the updated field data.

How to extract the user name and changed field? You receive this:

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

where "id" is my pageId and "changed_fields" is an array of changed fields. You can extract these as following:

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

Hope it helps! :)

No function -> the respose contain a array of array the correct code is:

$json = json_decode($updates["entry"][0]["uid"], true);
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
abhi
  • 1,412
  • 19
  • 25
  • thanks ... sin ... I needed that I return the id of the post published and instead does not allow it ... okay I will make a request / me / feed and will check the first post :) – Bonni Nov 24 '14 at 20:27
  • Edhiv ... what extract id of JSON ? ... i have a problem of extract id of user – Bonni Nov 24 '14 at 21:13
  • I have updated my answer. Changed "id" to "uid". If it helps then accept the answer. – abhi Nov 24 '14 at 21:24
  • Remember to add a limit when you a request /me/feed/. If you need only the first post then put limit as 1, this will give you the latest feed. – abhi Nov 24 '14 at 21:32
  • i have a problem of extract a information of response ... no undestand istruction for correct extract T.T – Bonni Nov 26 '14 at 21:00
  • Abhidav -> i receive a reespose contain a \" ... why ? – Bonni Nov 29 '14 at 09:38
  • receive a -> {"object":"user","entry":[{"uid":"281730338703668","id":"281730338703668","time":1417554005,"changed_fields":["feed"]}]} – Bonni Dec 04 '14 at 12:26
  • cool!! you are receiving the updates.. So what information do you want to extract? For json parser you can take a look at examples like this: http://stackoverflow.com/questions/4343596/parsing-json-file-with-php – abhi Dec 04 '14 at 22:04
  • thanks... extract uid and type of fields changed. But uid not present in ufficial Api o.O – Bonni Dec 16 '14 at 09:36