简体   繁体   English

如何使用Graph api在我们的朋友列表中搜索FB好友

[英]how to search FB friends in our friend list using Graph api

How to search our friends by name in our Facebook friend list? 如何在我们的Facebook好友列表中按姓名搜索我们的朋友?

In my program, I am listing all my friends using api in a box. 在我的程序中,我在一个盒子中列出了所有使用api的朋友。 In that box, there is one field- search for searching our friends. 在那个框中,有一个字段搜索我们的朋友。 I want to search my friends using Graph api. 我想使用Graph api搜索我的朋友。 But I don't know how to do that. 但我不知道该怎么做。

FB doesn't provide any api for searching a specific user's friends, but it's easy to do it by yourself. FB没有为搜索特定用户的朋友提供任何api,但是很容易自己做。 I recommend you use facebook-php-sdk . 我建议你使用facebook-php-sdk

1. 1。

require 'facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

$user = $facebook->getUser();
if ($user) {
    $friends = $facebook->api('/me/friends');
    // DO SOMETHING ELSE
}

from now on, $friends is a list of user's friend. 从现在开始, $friends是用户朋友列表。 it equals accessing https://graph.facebook.com/me/friends?access_token=USER_TOKEN manually. 它等于手动访问https://graph.facebook.com/me/friends?access_token=USER_TOKEN

2. suppose $target is a string from your search field 2.假设$target是搜索字段中的字符串

foreach ($friends["data"] as $value) {
    if (strpos($value["name"], $target) !== false) {
        // DO SOMETHING YOU WANT TO
    }
}     

By the way, using javascript to search friend on client side may be more efficient and dynamical than using php. 顺便说一句,使用javascript搜索客户端的朋友可能比使用PHP更有效和动态。 Personally, I would do it in the front end. 就个人而言,我会在前端做到这一点。 Suppose you are using jQuery, and target is a string from your search field. 假设您使用的是jQuery,而target是搜索字段中的字符串。

var url = 'https://graph.facebook.com/me/friends?access_token=USER_TOKEN'
$.getJSON(url, function(data) {
    friends = data['data'];

    for(i in friends){ 
        if (friends[i]['name'].search(target) != -1){
            // DO SOMETHING YOU WANT TO
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM