从facebook获取用户信息 [英] Retrieving user information from facebook

查看:419
本文介绍了从facebook获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Facebook在我的网站上注册.

I am doing a registration on my website via facebook.

当用户通过Facebook登录时,返回的$ user数组不完全是我想要的.

When the user logs in via facebook the $user array returned is not exactly what i want.

我浏览了可通过Facebook访问的用户参数,我也尝试实现它们,但是它不起作用.

I have gone through the user parameters that are accessible via facebook, i have tried implementing them also but it is not working.

这是我所拥有的样品

require_once "Database_Connect.php";
if (!isset($_POST['choosepassword']))
{
 # We require the library
 $user=array();
 require("facebook.php");
 # my error tracker
 $error=0;

 # Creating the facebook object
 $facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxx',
  'cookie' => true
 ));

 # Let's see if we have an active session
 $session = $facebook->getSession();

 if(!empty($session)) {
  # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
  try{
   $uid = $facebook->getUser();
   $user = $facebook->api('/me');
  } catch (Exception $e){}

  if(!empty($user)){
   # User info ok? Let's print it (Here we will be adding the login and registering routines)
   print_r($user);

   ***At this point what is retrieved is not exactly what i want*****

   $ue=$user['email'];$ui=$user['id'];
   $query = mysql_query("select * from members where email = '$ue' or (oauth_provider = 'facebook' AND oauth_uid = '$ui')", $link);
   $result = mysql_fetch_array($query);

   # If not, let's add it to the database
   if(!empty($result)){
    $error = 2; //record already in database
    require_once "facebook_error.php";
    die();
   }
  } else {
   # For testing purposes, if there was an error, let's kill the script
   $error = 1; //we were unable to retrieve info frm facebook
   require_once "facebook_error.php";
   die();
  }
 } else {
  # There's no active session, let's generate one
  $login_url = $facebook->getLoginUrl();

 *** I tried specifying what i want returned here, but it doesnt seem to work*****

  $url = $facebook->getLoginUrl(array(
  'req_perms' => 'uid, first_name, last_name, name, email, current_location, user_website, user_likes, user_interests, user_birthday, pic_big',
  'next' => 'http://www.zzzzzzz.com/facebook_register.php',
  'cancel_url' => 'http://www.zzzzzzz.com'

 ));
  header("Location: ".$login_url);
 }

我在做什么不对?

谢谢

更新

我现在正在使用FQL从Facebook中选择用户信息,

I am using FQL to select user info from facebook now,

$fql    =   "select uid, first_name, last_name, name, sex, email, current_location, website, interests, birthday, pic_big from user where uid=me()";
            $param  =   array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
            $user   =   $facebook->api($param);

它检索除生日电子邮件

如何选择电子邮件和生日?

How can i select the email and birthday?

谢谢

推荐答案

您的应用程序为此需要user_birthdayemail权限,否则它将不返回该信息.您只需要提供需要权限的参数,而不是req_perms参数中想要的字段,所以它应该像这样:

Your application needs the user_birthday and email permissions for this, else it will not return that information. You only need to supply parameters that need a permission, not what fields you want in the req_perms parameter, so it should look like this:

$url = $facebook->getLoginUrl(array(
  'req_perms' => 'email, user_birthday',
  'next' => 'http://www.zzzzzzz.com/facebook_register.php',
  'cancel_url' => 'http://www.zzzzzzz.com'
 ));

这篇关于从facebook获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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