0

我在这里找到了一些关于如何使用图形 api 进行编程的相关问题,但没有一个将文档与实际实现联系起来,这基本上就是这个问题的意义所在。

我正在尝试使用 php 构建一个 Facebook 应用程序。基本上,我想显示与您在同一所大学就读的所有朋友的列表。那么我将如何去做(我打算使用“教育”查询)?我已经让用户登录,我可以打印用户名。

我已经看到了大量关于如何执行 Facebook 查询的示例,如下所示:

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

那么我是否只是将该 URL 作为一行代码插入(如果是,我如何显示它 - 使用 echo?)?

我以前从未使用过 json 或 fql 编程,而且我是 php 新手。据说每个人都可以在https://developers.facebook.com/docs/reference/api/上观看视频并阅读该文档。不幸的是,该文档中的任何内容都没有在视频和如何实际编程之间建立任何联系。所以这不是很有帮助。但是,我确实觉得在图形 api 浏览器中的“GET/POST/DELETE”框之后生成的 url 必须有一些重要的东西。

那么我错过了什么?如何编写我的 php 代码来获取和打印去你大学的朋友列表?

我当前的 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>
4

1 回答 1

0

首先,您需要添加适当的权限以访问用户和用户朋友的教育历史,您可以通过这样做来实现,但请记住 user_education_history 是访问用户教育,而 friends_education_history 是用于用户朋友的教育历史:

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

要访问教育领域,您必须像这样修改代码中的一行: $user_profile = $facebook->api('/me?fields=name,education,email','GET');

您可以使用下面的代码来打印您的 $user_profile 然后根据您的要求对其进行解析:

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

对于您的查询,可能为时已晚,但可能对其他人有所帮助。

于 2013-10-31T00:43:03.460 回答