In Facebook graph API how to find which all of your friends like a particular book or movie

别等时光非礼了梦想. 提交于 2019-12-02 16:01:29

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:

  1. Get all the MOVIEs ids that your friends like
  2. Remove the repeated results and divide the resultant array to a set of arrays (10 movies each)
  3. Query Facebook again to get the friends for each set (10 movies at a time) which would hopefully get you the full list of friends
  4. sort by movie id

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!