Additional information in one API call

169 views Asked by At

Given below my code outputs number of Posts, Likes, Comments, and Shares for my Facebook page. It considers only present one week data. This is done using fql qpproach (I know fql is deprecated but my code works fine because the app I am using is an older app). I have few questions to ask and need help in getting that done:

  1. I need help in converting this code using the latest api approach so that it can work on newly created apps.
  2. The code outputs total number of likes, comments and shares just fine but is it possible to get the usernames of those who liked, commented and share the posts of my page in the past one week and update the values in a database table? If yes, how?
  3. If point #2 is possible, can this be done in one API call?

Appreciate some assistance here.

<?php
require('config.php');
session_start();
?>

<?php 
$facebook = new Facebook(array(
  'appId'  => '012myappid210',
  'secret' => 'abc012myappsecret210cba',
  'cookie' => true, 
));

$token=$_SESSION['token'];

$pageid='88978302070'; //(my facebook page id)
$d=strtotime("now");
$d2=strtotime('now - 7 days');

$fqlAPIParams = array(
  'method' => 'fql.query',
  'query' =>' 
  SELECT  post_id,comments,message,likes,created_time,share_count
  FROM    stream
  WHERE   actor_id = '.$pageid.' AND
  source_id = '.$pageid.' AND created_time  <= '.$d.' AND created_time >= '.$d2.' 
  LIMIT 250' ,
  'access_token'=>$token
);

$result = $facebook->api($fqlAPIParams);
$postCount = 0;
$likescount=0;
$commentscount=0;
$sharescount=0;

foreach($result as $post) 
{
  $shares=$post['share_count'];
  $likes=$post['likes']['count'];
  $comments=$post['comments']['count'];
  $likescount+=$likes;
  $commentscount+=$comments;
  $sharescount+=$shares;
  $postCount++;
}

echo "Post " . $postCount . " Likes " . $likescount . " Comments " . $commentscount . " Shares " . $sharescount;

?>

Here is the output of the code you suggested:

Post: Likes: 25 Comments: 5 Shares: 30
Post: Likes: 0 Comments: 0 Shares:
Post: Likes: 25 Comments: 25 Shares: 54

PageID 88978302070 has 2 posts in the past 30 days (on 31st Oct and 21st Oct)

Few changes what I made:
1. I changed 'until' to 'since' 88978302070/posts?fields=likes,comments,shares&since=-30 days.
2. I changed the inner loop because one more issue I found was... instead of $post['likes'] and $post['comments'], it should have been $post['likes']['data'] and $post['comments']['data']

Code is working but Now the problem is:
1. It lists three posts whereas the page has only 2. One additional post is shown with 0 likes, 0 shares, 0 comments. Not sure where it is coming from.
2. Like count is incorrect. It only displays and lists a maximum of 25 likes and comments. I tried to put a limit of 999999 but it displays maximum 1000. Is there any solution to this? Actual like count for the two posts are 24483
3. Comment count is incorrect. It has to be 90 but the code lists 25+5=30
4. It does not lists usernames of those who shared the posts.

1

There are 1 answers

9
Niraj Shah On

Without using FQL, you can do the following:

$d2 = strtotime( '- 7 days' );

$result = $facebook->api( $page_id . '/posts?fields=likes,comments,shares&until=' . $d2 );

foreach ( $result['data'] as $post ) {
  echo 'Post: ' . $post['id'] . ' Likes: ' . count( $post['likes']['data'] ) . ' Comments: ' . count( $post['comments']['data'] ) . ' Shares: ' . $post['shares']['count'] . '<br/>';
}

The above API result also includes the name and IDs for all the users who have liked / commented on each post. You can do another foreach loop in the code to cycle through the likes / comments and update your database accordingly.

Example:

// main loop to cycle through posts
foreach ( $result['data'] as $post ) {
  // inner loop to cycle through likes
  foreach ( $post['likes'] as $like ) {
    echo 'User: ' . $like['id'] . ' Name: ' . $like['name'];
    // code to check / update db here
  }
}

Running the above code for page 177526890164 (Narendra Modi) using limit=2 returns the following. If you are seeing a blank page or an error, you're clearly doing something wrong:

enter image description here

The best way to test would be to make the API call, and then add print_r( $result ) after to see if Facebook returns any data. If it's empty, there is something wrong with your API call.