我遵循了一个几乎可以正常运行的在线教程。该代码看起来像这样。尝试使用,require_once __DIR__ . "/Facebook/autoload.php";
但返回Fatal error: Class 'Facebook\FacebookSession' not found
。
编辑**添加了建议的autoload.php。
session_start();
require __DIR__ . "/facebook-php-sdk-v4-4.0-dev/autoload.php";
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\GraphLocation;
use Facebook\FacebookOtherException;
$appId = 'xxx';
$appSecret = 'xxx';
$redirect_url = 'http://example.com/com/login';
//initialize Facebook
FacebookSession::setDefaultApplication($appId, $appSecret);
$helper = new FAcebookRedirectLoginHelper($redirect_url);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
die(" Error : " . $ex->getMessage());
}
var_dump($_SESSION['fb_token']); //Notice: Undefined index: fb_token
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
$session = new FacebookSession($_SESSION['fb_token']);
}
if($session) {
//store token in php session
$_SESSION['fb_token'] = $session->getToken();
try {
$user = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className())->asArray();
} catch(FacebookRequestException $e) {
}
$name = $user['name'];
$firstName = $user['first_name'];
$lastName = $user['last_name'];
$fbId = $user['id'];
$fbEmail = $user['email'];
}else{
echo "No session!";
}
这是登录URL,位于每个页面的标题中。我不会包含完整的代码,但是只有未登录的访问者才能看到以下内容:
<?php
echo '<li class=" fb right"><a href="'.$helper->getLoginUrl().'" ><img style="height: 29px;" src="img/login_fb.png" alt="Facebook Log in" /></a> </li>';
?>
登录网址:
if( isset($_SESSION['fb_token']) ) {
//do stuff with user info
}else{
echo "no token";
}
no token
消息重定向到登录页面。如果在登录页面上再次单击登录URL,则一切正常。编辑**
当您拥有漂亮的(seo友好)URL时,Facebook PHP-Sdk的access_token会有一些问题。
使用FacebookJS-Sdk
来吸引用户access_token
是更好的选择。
获得用户权限后access_token
,使用将其传递到您的php文件(即facebook.php)AJAX
。
因此,您的index
文件如下所示:
<html xmlns:fb="http://ogp.me/ns/fb#">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<!----facebook js sdk---->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'PASTE YOUR APP ID',
cookie: true,
xfbml: true,
version: 'v2.1'
});
};
function Login() {
FB.login(function(response) {
if (response.authResponse) {
/*AJAX call to send access_token to php file.*/
jQuery.ajax({
url: 'facebook.php',
/*facebook.php is the file where we'll make API request to get user data.*/
type: 'POST',
data: {
'access_token': response.authResponse.accessToken /*this is the access_token provided by Facebook.We are passing it to our facebook.php file. */
},
success: function(result) {
var res = result;
if (res) {
document.getElementById("fb_status").innerHTML = res;
}
}
});
}
}, {
scope: 'email,user_friends,user_location,user_events,publish_actions'
});
}
/*function Logout(){
FB.logout(function(response) {
location.reload();
// Person is now logged out
});
}*/
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<!----------------------->
<div id="fb_status"></div> <!--Division to print response just to check every thing is working fine.-->
<a onclick="Login();">Sign In With Facebook</a> <!--Log-In/Sign-In Link-->
</body>
</html>
现在让我们看一下facebook.php
文件:
<?php
require_once('autoload.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
$app_id = 'PASTE YOUR APP ID HERE';
$app_secret = 'PASTE YOUR APP SECRET HERE';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$session = new FacebookSession($_POST['access_token']); /*$_POST['access_token'] is the access_token we sent to this file in AJAX call. */
/*So now we have your session , we can proceed further by making api request to get user data*/
try{
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className())->asArray();
/* `$me` will hold the user-data provided by Facebook.To check the data just dump this variable like this:*/
echo '<pre>';
var_dump($me);
echo '</pre>';
/*Now You have user data Do whatever you want to do with it.
Probably store user data in the DATABASE & START LOCAL SESSION*/
}catch(FacebookRequestException $e){
var_dump($e);
}
?>
注意:使用Facebook最新的PHP-Sdk:这是“ facebook-php-sdk-v4 ”的链接
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句