简体   繁体   English

使用Facebook的Graph API生成Facebook朋友列表

[英]Generating list of facebook friends using Facebook's Graph API

I've found a few related questions here on how to program with the graph api, but none of them connect the documentation to actual implementation, which is essentially what this one is about. 我在这里找到了一些有关如何使用图api进行编程的相关问题,但是没有一个问题将文档与实际实现联系起来,这实际上就是这个问题。

I'm trying to build a Facebook app with php. 我正在尝试使用php构建Facebook应用。 Basically I want to display a list of all the friends enrolled at the same college as you. 基本上,我想显示与您在同一所大学就读的所有朋友的列表。 So how would I go about doing that (I plan on using the "education" query)? 那么,我将如何去做(我打算使用“教育”查询)? I've already got the user to log in and I can print the user name. 我已经有该用户登录,并且可以打印用户名。

I've seen tons of examples of how to execute Facebook queries like this: 我已经看到了很多有关如何执行Facebook查询的示例,如下所示:

GET https://graph.facebook.com/me?fields=name,birthday,photos.limit(10).fields(id, picture)

So do I just plunk that URL in as a line of code (and if so, how do I display it - with echo?)? 那么,我是否只是将URL作为代码行插入(如果是,那么如何显示它-带有echo?)?

I've never programmed with json or fql before and I'm a php novice. 我以前从未编程过json或fql,而且我是php新手。 Everyone's said to watch the video at https://developers.facebook.com/docs/reference/api/ and read that documentation. 大家都说要在https://developers.facebook.com/docs/reference/api/上观看视频并阅读该文档。 Unfortunately nothing in that documentation makes any link between the video and how to actually program anything. 不幸的是,该文档中没有任何内容在视频与实际编程之间建立任何联系。 So that's not very helpful. 所以这不是很有帮助。 However I do feel like there has to be something important about that url generated right after the "GET/POST/DELETE" box in the graph api explorer. 但是,我确实觉得在图api资源管理器中的“ GET / POST / DELETE”框之后立即生成的URL必须具有一些重要意义。

So what am I missing? 那我想念什么呢? How do write my php code to get and print a list of friends who go to your college? 如何编写我的php代码以获取并打印上大学的朋友列表?

My current php code, for reference (CSS is from an external document on my server - there's some boilerplate text and boxes in there, I know!): 我当前的php代码,供参考(CSS来自服务器上的外部文档-我知道其中有一些样板文本和框!):

<?php
  // Setting up php sdk
  require_once('src/facebook.php');

  $config = array(
    'appId' => '196743413820733',
    'secret' => 'f39853b06a0607c1efd272d071c2f371',
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();

?>
<html>
  <head>
  <!-- Getting facebook-style CSS -->
  <link rel="stylesheet" type="text/css" href="mystyles.css" media="screen" />
  </head>
  <body>

  <?php
    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'];
        echo "<br/> Here are some things about yourself";

        echo '<div class="fbgreybox" style="width: 500px;"><p class="fbbody">
            Recognize this general purpose grey box? </p>
        </div>';

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

  </body>
</html>

First of all you need to add proper permission to access education history of user & user's friends, which you can achieved by doing this but remember user_education_history is to access User education while friends_education_history is for User's friends education history: 首先,您需要添加适当的权限才能访问用户和用户朋友的教育历史记录,您可以通过执行此操作来实现,但是请记住,user_education_history是访问用户教育,而friends_education_history是针对用户的朋友教育历史记录的:

$params = array(
'scope' => 'user_education_history, friends_education_history, user_location, email');
$loginUrl = $facebook->getLoginUrl($params);

And to access education field you have to modify one line from your code like this: $user_profile = $facebook->api('/me?fields=name,education,email','GET'); 要访问教育领域,您必须像这样修改代码中的一行:$ user_profile = $ facebook-> api('/ me?fields = name,education,email','GET');

You can use below code to just print your $user_profile and then parse it according to your requirements: 您可以使用以下代码仅打印$ user_profile,然后根据需要对其进行解析:

<pre><?php print_r($user_profile); ?></pre>

Probably its too late answer for your query but might be helpful for some other. 对于您的查询来说,答案可能为时已晚,但对其他查询可能会有帮助。

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

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