简体   繁体   中英

Adding Facebook Login to Own Website

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

I've downloaded the Facebook PHP SDK from developers.facebook.com and 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.

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


I have this is my header:

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


This is the code on the main page, where the login will be (also from 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) .

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 :

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) 
} 

i think this link help you for every question with using facebook php sdk http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-xx-which-uses-graph-api/

For wordpress plugin

http://wordpress.org/extend/plugins/facebookall/ By sourceaddons

i have also tried this

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 iframe that you can get from Facebook.

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

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".

Ex:

facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM