Enrico Susatyo
Enrico Susatyo

Reputation: 19790

How can I get friendship details in Facebook Graph API?

How can I get a friendship detail for two people? So for example in the web it will be:

http://www.facebook.com/<my_id>?and=<friend_id>

Is there any way I can do this in Graph API? Furthermore can I get specific items such as photos together, wall posts between us, etc? (Not documented AFAIK, but many Graph API features aren't anyway...)

EDIT: I think it should be possible with Graph API. For example getting family details (brother, sister, parents, etc) is not documented yet I still able to do it.

Upvotes: 14

Views: 9582

Answers (6)

St&#233;phane Bruckert
St&#233;phane Bruckert

Reputation: 22903

Getting the photos where two (or more) users are tagged in:

SELECT pid, src_big FROM photo 
    WHERE pid IN(
          SELECT pid FROM photo_tag WHERE subject=me()) 
      AND pid IN(
          SELECT pid FROM photo_tag WHERE subject=FRIEND_ID)
      AND pid IN(
          SELECT pid FROM photo_tag WHERE subject=ANOTHER_FRIEND_ID)
      ...

Upvotes: 0

Jean Ray Charlier
Jean Ray Charlier

Reputation: 41

This is what I have been using:

<?php
function main()
{
    global $Fb;
    $Fb = new Facebook(array('appId'=>FB_API_ID,'secret'=>FB_API_SECRET));
    $logoutUrl = $Fb->getLogoutUrl();
    $loginUrl = $Fb->getLoginUrl();
    $user = fb_loguser($Fb);
    if ($user)
    {
        $txt .= "<p align=\"center\"><a href=\"" . $logoutUrl . "\">Logout</a></p>";
        $txt .= "<h3 align=\"center\">You</h3>";
        $access_token = $Fb->getAccessToken();
        $user_profile = $Fb->api('/me');
        $txt .= "<p align=\"center\">
            <img src=\"https://graph.facebook.com/".$user."/picture\"></p>";
        $txt .= fb_friends_list($user_profile,$access_token);
    }
}   
function fb_loguser($facebook)
{
    global $Fb;
    $Fb = $facebook;
    $user = $Fb->getUser();
    if ($user) 
    {
        try
            $user_profile = $Fb->api('/me');
        catch (FacebookApiException $e) 
        {
            error_log($e);
            $user = null;
        }
    }
    return($user);
}
function fb_friends_list($access_token)
{
    global $Sess,$Fb;
    $friends = $Fb->api('/me/friends'); // return an array [data][0 to n][name]/[id]
    $siz = sizeof($friends['data']);
    $txt  = "<h3>Your FRIENDS on FACEBOOK</h3>";
    $txt .= "<table>";
    for ($i=0; $i<$siz; $i++)
    {
        $fid = $friends['data'][$i]['id'];
        $src = "http://graph.facebook.com/".$fid."/picture";
        $txt .= "<tr><td><img src=\"".$src."\" /></td>";
        $txt .= "<td>".$friends['data'][$i]['name'] . "</td>";
        $txt .= "<td>" . $fid . "</td></tr>";   
    }
    $txt .= "</table>";
    return($txt);
}
?>

Call main()!

Upvotes: 0

Bighnaraj
Bighnaraj

Reputation: 1

http://bighnarajsahu.blogspot.in/2013/03/facebook-graph-api-to-get-user-details.html

This is the complete code to get the friendlist in Fb.

Upvotes: -2

jpstrikesback
jpstrikesback

Reputation: 2308

If I understand properly you want to "View Friendship"

This type of query is not directly possible using the Graph API (at the moment). You would need to gather the information from each resource endpoint and then do some relating on your own part to "View Friendship"

Upvotes: 0

thaddeusmt
thaddeusmt

Reputation: 15600

Yes, I think you can also do the same thing phillee answered with the Graph API instead of FQL:

  1. Get user's photos https://graph.facebook.com/USERID/photos
  2. Get each photo's tags https://graph.facebook.com/PHOTOID/tags
  3. Sort through the list of photo tags, and grab all photos with the Friend in them
  4. Get each photo's comments https://graph.facebook.com/PHOTOID/comments
  5. Sort through the list of photo comments, and grab all comments left by the friend
  6. As the other answer also said: rinse and repeat for all data you want
    1. https://graph.facebook.com/USERID/feed
    2. https://graph.facebook.com/USERID/posts
    3. etc etc, see all connections here: http://developers.facebook.com/docs/reference/api/user/

For the interests, movies, activities, etc just make an API call for both (https://graph.facebook.com/ONEUSER/music and https://graph.facebook.com/OTHERUSER/music) and find the intersection of the two sets of data (loop through each list and save all matches to a separate list of mutual Likes)

There are no specific API calls for friendships though, so you will have to build your own. My guess is that Facebook is doing exactly this on their Friendship pages :)

It should be just as easy with FQL as with the REST API... maybe even easier with FQL since you can add WHERE conditions and get back just the data you need (SELECT * FROM comments WHERE fromid = FRIENDID), instead of sorting through the big list returned by the API (unless there is a way to add conditions to API request URLs?).

Upvotes: 3

phillee
phillee

Reputation: 2227

You can simulate a friendship query by doing multiple FQL queries. For example, to get all the photos between two friends A and B, you can:

  1. Get all photos (p) from A
  2. Get all tags of B in p (query on the photo_tags table)
  3. Get all comments made by B on p (query on the comments table)

Repeat, this time selecting photos from B.

You can apply the same concept on other things such as posts, likes, videos etc.

Upvotes: 6

Related Questions