php 将 Facebook 登录添加到自己的网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12925716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:29:15  来源:igfitidea点击:

Adding Facebook Login to Own Website

phpfacebooklogin

提问by iRector

I'd like users on my site to be able to log in using their existing Facebook accounts.

我希望我网站上的用户能够使用他们现有的 Facebook 帐户登录。

I've downloaded the Facebook PHP SDK from developers.facebook.comand set up an app for my website. I just used the example.php file that came bundled with the sdk, and swapped out the numbers for the 'appID', and 'secret' of my app.

我已经从developers.facebook.com下载了Facebook PHP SDK并为我的网站设置了一个应用程序。我只是使用了与 sdk 捆绑在一起的 example.php 文件,并交换了我的应用程序的“appID”和“秘密”的数字。

NOTE: In code 'appID' and 'secret' are replacing the actual equivalent

注意:在代码 'appID' 和 'secret' 中替换了实际的等价物



I have this is my header:

我有这是我的标题:

require 'src/facebook.php';(Which is definitely being found)

require 'src/facebook.php';(这肯定是被发现的)



This is the code on the main page, where the login will be (also from example.php):

这是主页上的代码,登录将在其中(也来自example.php):

<?php
$facebook = new Facebook(array(
'appId'  => 'appID',
'secret' => 'secret',
));

var_dump($facebook);
// Get User ID
$user = $facebook->getUser();

echo "<br />";
var_dump($user);

if ($user) {
try {
    $user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
}
}

if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}

$naitik = $facebook->api('/naitik');
?>

<?php var_dump($user); ?>


<h1>php-sdk</h1>

<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<?php echo "LoginURL:"; var_dump($loginUrl);?>
<div>
 <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>

</div>
<?php endif ?>

<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>


When I'm not logged into Facebook, and click the link, it takes me to a Facebook login page. After logging in, it then redirects back to the main site with new variables in the url (state, code, etc.). Once "logged" in, var_dump($user)just returns int(0).

当我没有登录 Facebook 时,单击该链接,它会将我带到 Facebook 登录页面。登录后,它会使用 url 中的新变量(状态、代码等)重定向回主站点。一旦“登录”,var_dump($user)就返回int(0).

Would anyone be able to point me in the right direction? I have no clue why it can't assign a user.

有人能指出我正确的方向吗?我不知道为什么它不能分配用户。



Result of dumping $facebook:

倾销的结果$facebook

object(Facebook)#182 (9) { 
["sharedSessionID:protected"]=> NULL 
["appId:protected"]=> string(15) "appID" 
["appSecret:protected"]=> string(32) "secret" 
["user:protected"]=> NULL ["signedRequest:protected"]=> NULL 
["state:protected"]=> NULL ["accessToken:protected"]=> NULL 
["fileUploadSupport:protected"]=> bool(false) 
["trustForwarded:protected"]=> bool(false) 
} 

回答by Jake

If you're not using the Facebook connectivity such as posting as a user or anything like that. You may want to just look into the register form iframethat you can get from Facebook.

如果您不使用 Facebook 连接,例如以用户身份发布或类似内容。您可能只想查看可以从 Facebook 获得的注册表 iframe

回答by SMSM

Its Happen to me and when i check my apache log i found this :

它发生在我身上,当我检查我的 apache 日志时,我发现了这个:

And i found its an SSL problems when communicate with Facebook via PHP SDK (using Curl). You have to set CURL_OPTS "CURLOPT_SSL_VERIFYPEER" to "false" .

当我通过 PHP SDK(使用 Curl)与 Facebook 通信时,我发现了它的 SSL 问题。您必须将 CURL_OPTS "CURLOPT_SSL_VERIFYPEER" 设置为 "false" 。

Ex:

前任:

facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;