Facebook PHP SDK - 将用户数据存储到MYSql数据库中 [英] Facebook PHP SDK - Store user data into MYSql database
问题描述
<?php
define('db_user','myUserName');
define('db_password','myPassword');
define('db_host','myHost');
define('db_name','myDbName');
//连接到MySQL
$ dbc = @mysql_connect(db_host,db_user,db_password)OR死('无法连接到MySQL:'mysql_error());
//选择正确的数据库
mysql_select_db(db_name)OR死('无法选择数据库:'mysql_error());
require'lib / facebook.php';
//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'MYAPPID',
'secret'=>'MYSECRET',
'cookie'=>真正));
//获取用户ID
$ user = $ facebook-> getUser();
//根据用户是否登录,我们可能有也可能没有这个数据。
//
//如果我们在这里有一个$ user id,那意味着我们知道用户登录到
// Facebook,但我们不知道访问令牌是否有效。如果用户注销了Facebook,则访问
// token无效。
if($ user){
try {
//继续知道你有一个登录的用户被认证。
$ fbuid = $ facebook-> getUser();
$ user_profile = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
error_log($ e);
$ user = null;
}
} else {
header('Location:index.php');
}
$ query = mysql_query(SELECT * FROM users WHERE oauth_provider ='facebook'AND oauth_uid =。$ user ['id']);
$ result = mysql_fetch_array($ query);
if(empty($ result)){
$ query = mysql_query(INSERT INTO users(oauth_provider,oauth_uid,username)VALUES('facebook',{$ user ['id' ]},'{$ user ['name']}'));
$ query = msyql_query(SELECT * FROM users WHERE id =。mysql_insert_id());
$ result = mysql_fetch_array($ query);
}
//根据当前用户状态,需要登录或注销url。
if($ user){
$ paramsout = array('next'=>'http://www.mywebsite.com/test/logout.php');
$ logoutUrl = $ facebook-> getLogoutUrl($ paramsout);
}
?>
上述页面被称为user_page.php,它是登录后用户被重定向到的位置。我尝试将数据库代码移动到index.php(用户进行登录的地方),但到目前为止,一切努力都没有成功。这是index.php的代码:
require'lib / facebook.php';
//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'MYAPPID',
'secret'=>'MYSECRET',
'cookie'=>真正));
//获取用户ID
$ user = $ facebook-> getUser();
如果($ user){
try {
//继续知道你有一个登录的用户被认证。
$ fbuid = $ facebook-> getUser();
$ user_profile = $ facebook-> api('/ me');
header('Location:user_page.php');
} catch(FacebookApiException $ e){
error_log($ e);
$ user = null;
}
}
//根据当前用户状态,需要登录或注销url。
if($ user){
$ logoutUrl = $ facebook-> getLogoutUrl();
} else {
$ loginUrl = $ face- user_groups,user_hometown,user_work_history,email',
'redirect_uri'=>'http://www.mywebpage.com/test/user_page.php')
);
}
?>
更新 * PROBLEM FIXED($ user_profile而不是$ user):
$ query = mysql_query(SELECT * FROM users WHERE oauth_provider ='facebook'AND oauth_uid = $ user_profile ['id']);
$ result = mysql_fetch_array($ query);
if(empty($ result)){
$ query = mysql_query(INSERT INTO users(oauth_provider,oauth_uid,username)VALUES('facebook',{$ user_profile ['id' ]},'{$ user_profile ['name']}'));
$ query = msyql_query(SELECT * FROM users WHERE id =。mysql_insert_id());
$ result = mysql_fetch_array($ query);
}
Facebook用户的数据代码存储在 $ user_profile
而不是 $ user
。
I'm totally new to PHP and MySQL but I'm implementing the Facebook PHP SDK in my website. Everything works fine so far but i'm struggling to add the user data to my database (MySQL). Everything I have is a number instead of the username and oauth_uid (I get the number 5 for both fields). This is the code:
<?php
define('db_user','myUserName');
define('db_password','myPassword');
define('db_host','myHost');
define('db_name','myDbName');
// Connect to MySQL
$dbc = @mysql_connect (db_host, db_user, db_password) OR die ('Could not connect to MySQL: ' . mysql_error() );
// Select the correct database
mysql_select_db (db_name) OR die ('Could not select the database: ' . mysql_error() );
require 'lib/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'MYAPPID',
'secret' => 'MYSECRET',
'cookie' => true));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fbuid = $facebook->getUser();
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}else{
header('Location: index.php');
}
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user['id']}, '{$user['name']}')");
$query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$paramsout = array('next'=>'http://www.mywebsite.com/test/logout.php');
$logoutUrl = $facebook->getLogoutUrl($paramsout);
}
?>
The above page is called user_page.php and it's where the user is redirected to after the login. I tried to move the "database" code to index.php (where the user does the login) but every effort so far has been unsuccessful. This is the code for index.php:
require 'lib/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'MYAPPID',
'secret' => 'MYSECRET',
'cookie' => true));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fbuid = $facebook->getUser();
$user_profile = $facebook->api('/me');
header('Location: user_page.php');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(Array('scope'=> 'user_interests,user_activities,user_education_history,user_likes,user_about_me, user_birthday, user_groups, user_hometown, user_work_history, email',
'redirect_uri' => 'http://www.mywebpage.com/test/user_page.php')
);
}
?>
UPDATE* PROBLEM FIXED ( $user_profile instead of just $user) :
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");
$query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
The Facebook user's data in your code is stored under $user_profile
and not $user
.
这篇关于Facebook PHP SDK - 将用户数据存储到MYSql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!