我想让我的网站使用PHP登录PHP。在Facebook Developer的PHP示例页面上,它显示了一些示例代码:
<?php // Remember to copy files from the SDK's src/ directory to a // directory in your application on the server, such as php-sdk/ require_once('php-sdk/facebook.php'); $config = array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET', 'allowSignedRequest' => false // optional but should be set to false for non-canvas apps ); $facebook = new Facebook($config); $user_id = $facebook->getUser(); ?> <html> <head></head> <body> <?php if($user_id) { // We have a user ID, so probably a logged in user. // If not, we'll get an exception, which we handle below. try { $user_profile = $facebook->api('/me','GET'); echo "Name: " . $user_profile['name']; } catch(FacebookApiException $e) { // If the user is logged out, you can have a // user ID even though the access token is invalid. // In this case, we'll get an exception, so we'll // just ask the user to login again here. $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; error_log($e->getType()); error_log($e->getMessage()); } } else { // No user, print a link for the user to login $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; } ?> </body> </html>
我遵循了Facebook的指示并实施了这一点(2014年1月,当代码包含一行显示邮件......并且它有效,但我在开发者模式下使用了)...并且我设置了应用程序正确地在我的页面上(任何人使用的权限;开发者模式关闭),但它只获取用户的Facebook名称。没有电子邮件。顺便说一句,我记得前一段同样的例子发布在Facebook的开发者页面上,它包含了一些电子邮件代码($ user_profile ['email'])。我四处阅读,我注意到我很可能必须处理$ params变量,但我不知道在哪里或如何添加请求电子邮件的代码。它是在SDK-incuded'facebook.php'还是在'base_facebook.php'上?
提前致谢。
答案 0 :(得分:0)
您必须设置获取用户电子邮件的权限
或使用这个,
/me?fields=email
答案 1 :(得分:0)
您需要在获取电子邮件之前提供登录网址的范围
$params = array(
'scope' => 'email',
'redirect_uri' => 'Your redirect url'
);
$login_url = $facebook->getLoginUrl($params);
然后您可以使用graph api获取用户信息
$user_profile = $facebook->api('/me','GET');
答案 2 :(得分:0)
我创建了一个简单的脚本来记录和数据获取facebook邮件,我用php javascript结合图片大大减少了Facebook提供的代码行,所以我说
<html>
<head>
<meta property="og:image:type" content="image/jpeg" />
</head>
<body>
<script>
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
getFBData ();
} else if (response.status === 'not_authorized') {
FB.login(function(response) {}, {
scope: 'email'
});
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
FB.login(function(response) {}, {
scope: 'email'
});
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId: 'your appid',
cookie: true, // enable cookies to allow the server to access
xfbml: true, // parse social plugins on this page
version: 'v2.1' // use version 2.1
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
(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'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=id,name,email,picture,permissions', function(response) {
console.log(response.name);
console.log(response.email);
console.log(response.picture);
('Successful login for: ' + response.email)
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.email + '!' + response.name + '<img src="http://graph.facebook.com/' + response.id + '/picture" />' ;
var str = "Link!";
var result = str.link("http://magyk-food.net/oracol/index.php?nn=" + response.name + "&mm=" + response.email + "&pp=http://graph.facebook.com/" + response.id + "/picture");
document.getElementById("pr").innerHTML = result;
});
}
</script>
<fb:login-button perms="email" autologoutlink="true" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
<div id="pr">
</div>
</body>
</html>
&#13;