我正在开展一个需要facebook集成的项目。 我已经尝试在Facebook上创建应用程序ID和密钥。 但它没有完成如何做到这一点 以及我将如何整合到我的项目中。
答案 0 :(得分:0)
您需要下载Facebook PHP SDK并构建脚本。这是重定向到登录的脚本:
<?php
session_start();
require_once("lib/src/Facebook/autoload.php");
// 1. init facebook
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET
]);
// 2. Login (session_start must be called before)
$helper = $fb->getRedirectLoginHelper();
if ($_GET['code']){ // 4. IF code existing in the url, it is a redirect (after facebook login)
try {
// 5. Get Access Token
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// 6. Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// 7. Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
try{
// YOU CAN DO SOMETHING WITH FACEBOOK API HERE
$response = $fb->get('{DO_SOMETHING_HERE}', $accessToken);
$result = $response->getDecodedBody();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
} catch(Facebook\Exceptions\FacebookSDKException $e) {
}
}
} else {
// 3. redirect to facebook link with permission and callback url
$redirect_url = {URL_OF_THIS_PAGE};
$permissions = ['email','publish_actions','public_profile']; // optional
$loginUrl = $helper->getLoginUrl($redirect_url, $permissions);
header ("Location: ".$loginUrl);
exit;
}
?>