先在根目錄建立 composer.json
內容填入
1
2
3
4
5
|
{
"require"
: {
"facebook/php-sdk-v4"
:
"4.0.*"
}
}
|
接著執行 composer install
,系統會自動建立 vendor 目錄。在 application/libraries
建立 lib_login.php
檔案,並且寫入底下程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?php
if
(! defined(
'BASEPATH'
))
exit
(
'No direct script access allowed'
);
/**
* Name: Facebook Login Library
*
* Author: appleboy
*
*/
require
'vendor/autoload.php'
;
use
Facebook\FacebookSession;
use
Facebook\FacebookRequest;
use
Facebook\GraphUser;
use
Facebook\FacebookRequestException;
use
Facebook\FacebookRedirectLoginHelper;
class
Lib_login
{
/**
* CodeIgniter global
*
* @var string
**/
protected
$ci
;
/**
* __construct
*
* @return void
* @author Ben
**/
public
function
__construct()
{
$this
->ci =& get_instance();
$this
->ci->load->library(
'session'
);
$this
->ci->config->load(
'facebook'
);
if
(! isset(
$_SESSION
)) {
session_start();
}
}
public
function
facebook()
{
$facebook_default_scope
=
explode
(
','
,
$this
->ci->config->item(
"facebook_default_scope"
));
$facebook_app_id
=
$this
->ci->config->item(
"facebook_app_id"
);
$facebook_api_secret
=
$this
->ci->config->item(
"facebook_api_secret"
);
// init app with app id and secret
FacebookSession::setDefaultApplication(
$facebook_app_id
,
$facebook_api_secret
);
// login helper with redirect_uri
$helper
=
new
FacebookRedirectLoginHelper(site_url(
'login/facebook'
));
// see if a existing session exists
if
(isset(
$_SESSION
) && isset(
$_SESSION
[
'fb_token'
])) {
// create new session from saved access_token
$session
=
new
FacebookSession(
$_SESSION
[
'fb_token'
]);
// validate the access_token to make sure it's still valid
try
{
if
(!
$session
->validate()) {
$session
= null;
}
}
catch
(Exception
$e
) {
// catch any exceptions
$session
= null;
}
}
if
(!isset(
$session
) ||
$session
=== null) {
// no session exists
try
{
$session
=
$helper
->getSessionFromRedirect();
}
catch
(FacebookRequestException
$ex
) {
// When Facebook returns an error
// handle this better in production code
print_r(
$ex
);
}
catch
(Exception
$ex
) {
// When validation fails or other local issues
// handle this better in production code
print_r(
$ex
);
}
}
// see if we have a session
if
(isset(
$session
)) {
// save the session
$_SESSION
[
'fb_token'
] =
$session
->getToken();
// create a session using saved token or the new one we generated at login
$session
=
new
FacebookSession(
$session
->getToken());
// graph api request for user data
$request
=
new
FacebookRequest(
$session
,
'GET'
,
'/me'
);
$response
=
$request
->execute();
// get response
$graphObject
=
$response
->getGraphObject()->asArray();
$fb_data
=
array
(
'me'
=>
$graphObject
,
'loginUrl'
=>
$helper
->getLoginUrl(
$facebook_default_scope
)
);
$this
->ci->session->set_userdata(
'fb_data'
,
$fb_data
);
}
else
{
$fb_data
=
array
(
'me'
=> null,
'loginUrl'
=>
$helper
->getLoginUrl(
$facebook_default_scope
)
);
$this
->ci->session->set_userdata(
'fb_data'
,
$fb_data
);
}
return
$fb_data
;
}
}
|
最後寫簡單 controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
if
( ! defined(
'BASEPATH'
))
exit
(
'No direct script access allowed'
);
class
Login
extends
CI_Controller
{
public
function
__construct()
{
parent::__construct();
$this
->load->library(
array
(
'session'
,
'lib_login'
));
}
/**
* facebook login
*
* @return void
* @author appleboy
**/
public
function
facebook()
{
$fb_data
=
$this
->lib_login->facebook();
// check login data
if
(isset(
$fb_data
[
'me'
])) {
var_dump(
$fb_data
);
}
else
{
echo
'<a href="'
.
$fb_data
[
'loginUrl'
] .
'">Login</a>'
;
}
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
|
打開瀏覽器,直接執行 http://xxxx/login/facebook
就可以看到 Facebook 登入連結。所以程式碼都放在 Github 上 codeigniter-facebook-php-sdk-v4,歡迎取用。