O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Friends?

Problem

I need to determine whether two (or more) users are friends.

Solution

Use the Friends.areFriends() method:

$areFriends = $facebook->api_client->friends_areFriends('12345', '67890');

The method also accepts two arrays and will let you know whether each pair (pulling the member at the same position in each array) is a friend:

$aUsers = array('12345', '67890', '11223');
$bUsers = array('33445', '66778', '99100');
$areFriends = $facebook->api_client->friends_areFriends($aUsers, $bUsers);

Discussion

Either way, the Friends.areFriends() returns a multidimensional array in which each element contains uid1, uid2, and are_friends elements (with the latter being true, false, or empty if the members aren’t visible due to privacy rules or don’t exist, which is the case with our fictional users here):

Array
(
    [0] => Array
        (
            [uid1] => 12345
            [uid2] => 33445
            [are_friends] =>
        )

    [1] => Array
        (
            [uid1] => 67890
            [uid2] => 66778
            [are_friends] =>
        )

    [2] => Array
        (
            [uid1] => 11223
            [uid2] => 99100
            [are_friends] =>
        )
)

Since the method is symmetric, both arrays need to have the same number of elements in them or you’ll get an invalid parameter error.

FQL equivalent

If you’d prefer to use FQL to access friends, the equivalent query is:

SELECT uid1, uid2 FROM friend WHERE uid1=uid1 AND uid2=uid2

See Checking Whether Two Users are Friends for more information.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required