启动应用程序之前,如何检查Facebook用户ID是否已存在? [英] How to check Facebook User's ID already exists before lauching app?

查看:69
本文介绍了启动应用程序之前,如何检查Facebook用户ID是否已存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短说明..我正在为 Facebook 开发Flash游戏.用户启动应用程序时,我需要首次显示注册表.它应该检查用户是否已经注册(数据库中已存在).

Short description. I'm developing flash game for Facebook. I need to show registration form first time when user launching application. It should check If user already registered (existing in database).

目的.要实现此目的,我需要获取 Facebook用户ID 并在数据库中检查 Facebook用户ID 是否已经存在,然后再提供任何动作.

Purpose. To achieve this I need to get Facebook User's ID and check in database if Facebook User ID already exsists, before any providing any action.

问题.如何获取 Facebook用户ID 并将其发送到registration.php?我应该有单独的文件来检查用户的存在,打开应用程序,获取 Facebook用户ID 吗?

Question. How to get Facebook User's Id and send it to registration.php? Should I have seperate files for checking user's existance, opening app, getting Facebook User's Id?

我尝试过的事情:到目前为止,我已经尝试过类似的事情(代码注释中的一些解释):

What I've tried: For now I've tried something like that (some explaination in code's comments):

FacebookPermissions.php

 include_once "php-sdk/src/facebook.php";
 $app_id = 'xxxxx';
 $app_url = 'http://www.facebook.com/myPage/app_xxxxx?ref=ts';
 $facebook = new Facebook(array(
  'appId'  => 'your app id',
  'secret' => 'your app secret'
 ));

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

      // here should go code to check database with Facebook User's ID
      // I don't have ideas how to achieve It correctly

      $user_profile = $facebook->api('/me');
      $id = $user_profile['id'];
      session_start();
      $_SESSION['id'] = $id;

      // here should redirect to php file where is MySql query for checking?
      // if yes, how to redirect It correctly?

 }
 else
 {
       // this part is to ask permissions for user

       $loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" . 
       $app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
       echo("<script> top.location.href='" . $loginUrl  . "'</script>");
 }

这是php脚本,用于从表单获取数据并将其插入数据库,但是仅当数据库中不存在用户时才应使用.并且仅在这种情况下(如果用户不存在)应显示所有表单.

This is php script to get data and from form and insert It to database, but It should be used only If user not exists in database. And at all form should be shown only in that case (if user not exists).

InsertData.php

<?php 
session_start();
$id = $_SESSION['id']; 
$first_name = $_SESSION['first_name']; 
$last_name = $_SESSION['last_name']; 
$city = $_SESSION['city']; 
$email = $_SESSION['email']; 

$mysqli = new mysqli("localhost","xxx","xxx","xxx");
if ($stmt = $mysqli->prepare("INSERT INTO users (FB_Id, First_Name, Last_Name, City, Email) VALUE (?,?,?,?,?) ")) 
{
    if (!$mysqli->set_charset("utf8")) 
    {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
    } 
    $stmt->bind_param('sssss', $id, $first_name, $last_name, $city, $email);

    $stmt->execute();

    if ($stmt->error != '') 
    {
       echo ' error:'.$stmt->error;
    } else 
    {
       echo 'success';
    }
    $stmt->close();
} else 
{
   echo 'error:'.$mysqli->error;
}
$mysqli->close();

推荐答案

要获取用户的Facebook ID,您需要进行Graph API调用.要进行该API调用,首先需要通过调用代码的这一部分来请求用户许可:

To get the user's Facebook ID, you need to make Graph API call. To make that API call, first you need to ask user permission by calling this part of your code:

   // this part is to ask permissions for user

   $loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" . 
   $app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
   echo("<script> top.location.href='" . $loginUrl  . "'</script>");

只有在用户授予许可后,您才能通过调用代码的这一部分来获取用户的Facebook ID

Only after the user give permission, you can get the user Facebook ID by calling this part of your code

  $user_profile = $facebook->api('/me');

这篇关于启动应用程序之前,如何检查Facebook用户ID是否已存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆