FB API插入属于Open User帐户的user_id

时间:2013-03-22 08:51:58

标签: php facebook facebook-graph-api facebook-php-sdk

这是我在使用FB api时遇到的最奇怪的问题。也许我只是用他们的API太多了n00b,但是这里有。

我正在使用我网站的登录按钮。单击后,将向用户显示FB弹出窗口,告诉他们有关我的应用程序的基本信息(基本详细信息,活动用户数和请求的权限)

这一切都很好,花花公子。如果用户接受该应用程序,则将重新加载该页面,并且将从应用程序请求的信息将插入到数据库中并创建会话。

session_start();
require_once("model/functions.php");
require_once("controller.php"); 
require_once("model/facebook_api/src/facebook.php");

$facebook = new Facebook(array(
            'appId' => '123456789',
            'secret' => '123456789',
            'cookie' => true
        ));

        $access_token = $facebook->getAccessToken();
        $facebook->setAccessToken($access_token);

        if($access_token != "") 
        {
            $user = $facebook->getUser();

            if($user != 0)
            {
                $user_profile = $facebook->api("/".$user);  

                $fb_id = $user;
                $fb_first_name = $user_profile['first_name'];
                $fb_last_name = $user_profile['last_name'];
                $fb_email = $user_profile['email'];

//              The FB user_id and all of their other info is correct. Seriously!! 
                echo $fb_id."<br />";
                print_r($user_profile);

//              Query the DB to see if this person's info from FB is in there
                $query = "SELECT * 
                        FROM users 
                        WHERE fb_id = :fb_id";
                $stmt = $db->prepare($query);
                $stmt->execute(array(':fb_id' => $fb_id));
                $count = $stmt->rowCount();

                if($count == 1)
                {
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
                    {
                        $_SESSION['id']      = $row['user_id'];
                        $_SESSION['fb_id']   = $row['fb_id'];
                        $_SESSION['first']   = $row['first_name'];  
                        $_SESSION['last']    = $row['last_name'];
                        $_SESSION['email']   = $row['email'];   
                        $_SESSION['photo']   = $row['photo'];   
                        $_SESSION['accuracy'] = $row['accuracy_rate'];
                    }
                } else
                {   
                    $img_data = file_get_contents('https://graph.facebook.com/'.$fb_id.'/picture?type=large');
                    $save_path = 'img/profile_pics/large/';
                    file_put_contents($save_path.''.$fb_id.'.jpg', $img_data);

                    $insert = "INSERT INTO 
                            users 
                            (first_name, 
                            last_name, 
                            email, 
                            photo, 
                            fb_id, 
                            accuracy_rate, 
                            date_joined, 
                            date_joined_int) 
                            VALUES 
                            (:first_name, 
                            :last_name, 
                            :email, 
                            :photo, 
                            :fb_id, 
                            :points, 
                            :date_joined, 
                            :date_int)";    
                    $params = array(':first_name' => $fb_first_name, 
                                ':last_name' => $fb_last_name, 
                                ':email' => $fb_email, 
                                ':photo' => $fb_id.'.jpg', 
                                ':fb_id' => $fb_id,
                                ':points' => '100',
                                ':date_joined' => date("M j, Y"),
                                ':date_int' => idate('z'));
                    $stmt = $db->prepare($insert);
                    $stmt->execute($params)or die('error');

                    print_r($params);
//                      Query the DB to see if this person's info from FB is in there
                    $query = "SELECT * 
                            FROM users 
                            WHERE fb_id = :fb_id";
                    $stmt = $db->prepare($query);
                    $stmt->execute(array(':fb_id' => $fb_id));

                    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
                    {
                        $_SESSION['id']      = $row['user_id'];
                        $_SESSION['fb_id']   = $row['fb_id'];
                        $_SESSION['first']   = $row['first_name'];  
                        $_SESSION['last']    = $row['last_name'];
                        $_SESSION['email']   = $row['email'];   
                        $_SESSION['photo']   = $row['photo'];   
                        $_SESSION['accuracy'] = $row['accuracy_rate'];
                    }
                }
            } else
            {

            }
        }  else
        {

        }

每次刷新页面时都会在DB中插入一行。没有创建任何会话。更奇怪的是,插入数据库中fb_id列的facebook id属于一个帐户,该帐户属于FB用于测试提交审批的开放图形操作的测试帐户。我知道,因为在深入了解数据库之后,我在DB中看到了一行属于FB开放图操作测试人员。 ID为“2147483647”。这显然不是打印出$ user_profile数组时打印出来的ID。此外,在第一次重新加载页面之后,甚至不应该发生插入查询,因为返回的rowCount设置为1.如果用户是第一次用户,则应该执行插入查询。沙箱模式未启用。我想可能与它有关。但是,事实并非如此。

在一个不相关的说明中,FB的api在执行

时是否存在错误
$facebook->api("/me");

这似乎不起作用。

$facebook->api("/".$user); 

似乎工作得很好。

可以找到实例[{3}}

1 个答案:

答案 0 :(得分:0)

Facebook user_id可能超出支持的整数长度。在iPhone SDK上发现了一个长ID,例如。 '10000328862128237'。我怀疑这也是你的问题,因为我得到完全相同的数字'2147483647'。