Facebook PHP SDK v5 升級

05-19-2017

Facebook PHP SDK v5 升級

到新公司報到第一天,公司就告知最近網站FB login無法使用,希望我可以盡快確認為何無法使用;連上服務器把相關的檔案都下載回來,打開一看SDK v4、API v2.2,上網查了些資料,才知道API v2.2已經使用過期了,不能再使用這個版本,雖然FB會自動升級API版本,但因為SDK的版本也是舊的,新版的API以SDK v5進行開發,看到這也只好乖乖的把SDK跟API都升級。

Facebook 開放平台變更紀錄

升級準備

FB Login

設定FB AppID,在SDK v5的版本中,API預設會使用目前最低的版本,建議使用'default_graph_version' => 'v2.8'自行設定最新的

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
v4.0:

session_start();

// Facebook app settings
$app_id = '';
$app_secret = '';
$redirect_uri = '';

// Requested permissions for the app - optional.
$permissions = array(
'email',
'user_location',
'user_birthday'
);

// Define the root directoy.
define( 'ROOT', dirname( __FILE__ ) . '/' );

// Autoload the required files
require_once( ROOT . 'vendor/autoload.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphUser;

// Initialize the SDK.
FacebookSession::setDefaultApplication( $app_id, $app_secret );



v5.0:

session_start();

// Include the required Composer dependencies.
require_once( 'vendor/autoload.php' );

// Initialize the Facebook PHP SDK v5.
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.3',
]);

取得登入網址:

1
2
3
4
5
6
7
8
9
10
11
v4.0:

// Initialize the Facebook SDK.
FacebookSession::setDefaultApplication( $app_id, $app_secret );
$helper = new FacebookRedirectLoginHelper( $redirect_uri );


v5.0:

// Check if the user is logged in.
$helper = $fb->getRedirectLoginHelper();

登入完成取得 access_token:

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
v4.0:

// Authorize the user.
try {
if ( isset( $_SESSION['access_token'] ) ) {
// Check if an access token has already been set.
$session = new FacebookSession( $_SESSION['access_token'] );
} else {
// Get access token from the code parameter in the URL.
$session = $helper->getSessionFromRedirect();
}
} catch( FacebookRequestException $ex ) {

// When Facebook returns an error.
print_r( $ex );
} catch( \Exception $ex ) {

// When validation fails or other local issues.
print_r( $ex );
}
if ( isset( $session ) ) {

// Retrieve & store the access token in a session.
$_SESSION['access_token'] = $session->getToken();

$logoutURL = $helper->getLogoutUrl( $session, 'http://your-app-domain.com/logout' );

// Logged in
echo 'Successfully logged in! <a href="' . $logoutURL . '">Logout</a>';
} else {

// Generate the login URL for Facebook authentication.
$loginUrl = $helper->getLoginUrl();
echo '<a href="' . $loginUrl . '">Login</a>';
}


v.5.0:

try {
$accessToken = $helper->getAccessToken();
} catch( Facebook\Exceptions\FacebookSDKException $e ) {

// There was an error communicating with Graph
echo $e->getMessage();
exit;
}

if ( isset( $accessToken ) ) {

// User authenticated your app!
// Save the access token to a session and redirect
$_SESSION['facebook_access_token'] = ( string ) $accessToken;

// Register or log the user in...
exit;
}
elseif ( $helper->getError() ) {

// The user denied the request
// You could log this data . . .
var_dump( $helper->getError() );
var_dump( $helper->getErrorCode() );
var_dump( $helper->getErrorReason() );
var_dump( $helper->getErrorDescription() );

// You could display a message to the user
// being all like, "What? You don't like me?"
exit;
}

// If they've gotten this far, they shouldn't be here
http_response_code(400);
exit;

修改心得

這次修改API直接使用v2.8版本,再次碰到類似問題,應該是兩年後的事了,下次修改或許會把SDK再升級一次吧,至少修改完畢沒碰到什麼太大的問題;唯一問題是修改好,還沒保存下來,我的mac竟然出問題只能送修,默默的把問題重新再邊做邊找印象中的資料,來把問題修復,這告訴我之後要做好備份的習慣。

參考文章:

[Facebook SDK for PHP v5.0]を利用してログイン認証をおこなう[Oauth]
[PHP] Facebook Graph API v2.2 升級提醒 - CodeIgniter 2.x 與 Facebook PHP SDK v5
Login with Facebook using PHP