I'm trying to figure out how to find how many of my friends like a particular entity.
For example, for a given book or movie, how can I look up how many of my friends have already liked the same book or movie.
I believe that information does appear when we display the fb:like button social plugin but I want to get that count so I can programatically display the most popular books among my friends etc is descending order.
My backend is php but I'm also using Javascript SDK. Thanks for your inputs in advance
The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.
Just go to the Settings section of Facebook (the arrow next to the question mark, top right) and click on "download my data" at the bottom of the page called "General Account Settings." Then you'll make your request and await Facebook's compiling of your data, which is delivered via an email link.
If you ever need to re-enable the like counts, open the same “Reaction Preferences” window. In the window, toggle off both “On Posts from Others” and “On Your Posts” options. Facebook will start showing the like counts in your account.
These "objects" are actually pages, so to retrieve your friends list that like a movie or a book (a page), use this query:
SELECT uid FROM page_fan WHERE page_id = 91290503700 AND uid IN (SELECT uid2 FROM friend WHERE uid1=me())
Try this in the console of fql.query, it will retrieve all your friends ID that likes the Inception movie.
EDIT:
The obvious way to retrieve the movies (for example) that your friends like would be:
SELECT page_id,uid
FROM page_fan
WHERE type="MOVIE"
AND uid IN (
SELECT uid2
FROM friend
WHERE uid1=me()
)
But I've noticed that it won't return all the users if the set is large (which most likely the case for every user)..so I've made a hack just to get you started!
function sectionArray($array, $step) {
$sectioned = array();
$k = 0;
for ( $i=0;$i < count($array); $i++ ) {
if ( !($i % $step) ) {
$k++;
}
$sectioned[$k][] = $array[$i];
}
return $sectioned;
}
$result = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT page_id FROM page_fan WHERE type='MOVIE' AND uid IN (SELECT uid2 FROM friend WHERE uid1=me())"
));
$pages = array();
foreach($result as $k=>$v) {
$pages[] = $v["page_id"];
}
$pages = array_unique($pages);
$pages = array_values($pages);
$sets = sectionArray($pages,10);
$movies = array();
foreach($sets as $set) {
$page_set = implode(',',$set);
$friends = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT page_id,uid FROM page_fan WHERE page_id IN ($page_set) AND uid IN (SELECT uid2 FROM friend WHERE uid1=me())"
));
$movies[] = $friends;
}
$final = array();
foreach($movies as $v)
foreach($v as $k=>$arr)
$final[$arr["page_id"]][] = $arr["uid"];
print_r($final);
This code is doing the following:
And the result would be something like:
Array
(
[movie_id] => Array
(
[0] => friend_id
[1] => friend_id
[2] => friend_id
[3] => friend_id
)
[movie_id] => Array
(
[0] => friend_id
[1] => friend_id
[2] => friend_id
[3] => friend_id
[4] => friend_id
[5] => friend_id
[6] => friend_id
[7] => friend_id
)
[movie_id] => Array
(
[0] => friend_id
)
[movie_id] => Array
(
[0] => friend_id
)
)
From here, you can check for the most liked...etc
P.S.: As I said the above just to get you started and I guess you can cache some queries and improve the performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With