使用Facebook个人资料数据的推荐系统

时间:2011-10-21 21:33:45

标签: facebook facebook-graph-api

所以我正在创建一个基于Facebook个人资料数据发布推荐的推荐系统。因此,我为用户设置了一个基本框架,通过Facebook连接登录,授权应用程序权限,并登陆重定向页面,该页面将JSON中的用户配置文件数据显示为一系列数组(我已在下面粘贴) )。所以现在我需要创建一个基于特定数据发布建议的设置。例如:如果用户去了这所学校,或者主修这个,或者住在这里,或者这个老了,推荐这个。我习惯用PHP中的变量来做这件事,通常是通过手动提交的表格。那么如何将这个数据输出(我显然不希望在最终产品中显示在浏览器上)转换为服务器端的变量,这样我就可以创建if else语句来发布建议。我会使用Facebook对象ID号码而不是文本,即我会通过它的Facebook ID识别学校或专业,而不是像UCF或财务这样的实际名称。

所以我基本上要完成两件事。 1.从浏览器中取出数据并在PHP中在服务器端进行分析。 2.将数据转换为某种形式,我可以单独(和统称)在if_else语句中调用,以发出if语句表达式(最好是在PHP中)。现在,下面的数据是通过$ userInfo集体调用的,因此无法单独调用任何数据。

我从用户配置文件中调用了相当多的数据,因此我知道这是一项艰巨的任务,而且我对编程相对较新(约9个月)。我似乎完全迷失了。任何指导都会非常感激。

当前如何通过Graph API调用数据:

if ($user){
        //get user basic description
        $userInfo           = $facebook->api("/$user");

如何在客户端显示数据:

 <td>
                <!-- Data retrived from user profile are shown here -->
                <div class="box">
                    <b>User Information using Graph API</b>
                    <?php d($userInfo); ?>
                </div>
            </td>

以下输出:

Array
(
    [id] => 619011251
    [name] => Matt McClintock
    [first_name] => Matt
    [last_name] => McClintock
    [link] => http://www.facebook.com/MattMcClint
    [username] => MattMcClint
    [birthday] => 08/15/1988
    [hometown] => Array
    (
        [id] => 102184499823699
        [name] => Montreal, Quebec
    )

[location] => Array
    (
        [id] => 108288992526695
        [name] => Orlando, Florida
    )

[education] => Array
    (
        [0] => Array
            (
                [school] => Array
                    (
                        [id] => 111894272160018
                        [name] => Spanish River Community High School
                    )

                [year] => Array
                    (
                        [id] => 137616982934053
                        [name] => 2006
                    )

                [type] => High School
            )

        [1] => Array
            (
                [school] => Array
                    (
                        [id] => 35078114590
                        [name] => University of Central Florida
                    )

                [year] => Array
                    (
                        [id] => 118118634930920
                        [name] => 2012
                    )

                [concentration] => Array
                    (
                        [0] => Array
                            (
                                [id] => 104076956295773
                                [name] => Computer Science
                            )

                        [1] => Array
                            (
                                [id] => 107870585903083
                                [name] => Finance
                            )

                    )

                [type] => College
            )

    )

[gender] => male
[email] => matt@smartkrawl.com
[timezone] => -4
[locale] => en_US
[verified] => 1
[updated_time] => 2011-10-16T17:45:56+0000
)

1 个答案:

答案 0 :(得分:0)

经过一些故障排除后,我发现了一个简单但难以实施的解决方案。首先,您需要立即初始化SDK,如下所示:

<?php
require_once('php-sdk/facebook.php');

$config = array(
'appId' => 'XXXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXX',
);

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

然后我们确保用户当前已登录并提示他们登录,如果他们不是:

<?
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');


  } 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>';

}

?>

现在您可以调用Facebook用户个人资料中的任何数据,如下所示:

<?php
if ($user_profile['education'][0]['school']['id']=='111894272160018') {
    echo "Finally. It worked";
}