Enabling facebook login using facebook php-sdk (v.3.0.0), with sessions and cookies

I am new to PHP and it is very difficult for me to understand facebook login system.

I downloaded three src / gitub files (https://github.com/facebook/php-sdk/). I tried using the example.php file to get started. However, I'm not sure what to do with it.

For those unfamiliar with the file, here is a copy of example.php, with the style removed:

require '../src/facebook.php'; $facebook = new Facebook(array( 'appId' => '...', 'secret' => '...', )); $user = $facebook->getUser(); 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'); ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <body> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a> <?php else: ?> <div> Login using OAuth 2.0 handled by the PHP SDK: <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']; ?> </body> </html> 

Here are my questions about this:

1) What about cookies? - I want the user to be able to log into my site after re-opening his browser.

2) What is the minimum minimum I need to get from this example.php file to check / register a user, start a session, save the session in a cookie, get the user fb user ID, name fb, fb image and friend list fb?

3) There is one fb_ca_chain_bundle.crt file in src / files, and I am completely unfamiliar with what a file is, and I'm not sure if it is even necessary. What is his purpose?

4) Line $naitik = $facebook->api('/naitik'); is the "naitik" username of this person, so if I type facebook.com/naitik, will he show his public profile? replaces "/ naitik" with "/ me", what will get a public profile of a person registered on facebook?

5) How to get an access token and how to use it in my code?

6) When I create a session for the user and a cookie so that the user logs in after re-opening the browser, what should I accurately store cookies in my sessions?

I know this is a lot of questions, but I looked through a lot of guides on the Internet, and none of them did a good job explaining this, mainly because they simply reference the Github PHP-SDK files. In addition, most of them explain the previous version of the PHP-SDK. Any help is appreciated with any of the questions.

+6
source share
1 answer

To answer your questions

1) How about cookies?

You simply add the parameter to the initialization of Facebook. Change it to the following

 $facebook = new Facebook(array( 'appId' => '...', 'secret' => '...', 'cookie' => true, )); 

2) What is the minimum minimum I need to exit this example.php file ....

In this example, not everything you need. The top of the code shows how to connect and verify the user. The second half simply dumps its main details and the details of the whitewater. For the rest you need to look further.

3) There is one file in src / files fb_ca_chain_bundle.crt, '

The purpose of this file is to suggest a workaround for the CURL 60 error. Read this:

http://www.takwing.idv.hk/blog/2011/php-sdk-demystified-%E2%80%93-how-curl-error-60-is-handled/

4) Line $ naitik = $ facebook-> api ('/ naitik'); this is the "naitik" username of this person, so if I type facebook.com/naitik, will he show his public profile? replaces "/ naitik" with "/ me", what will get a public profile of a person registered on facebook?

Exactly right

5) How to get an access token and how to use it in my code?

$ facebook-> getAccessToken ();

You add some method calls, but this is not necessary for everyone.

6) When I create a session for the user and a cookie so that the user logs in after re-opening the browser, what should I accurately store cookies in my sessions?

The SDK for Facebook will take care of this. You just need to store additional user information that your application requires.

+7
source

Source: https://habr.com/ru/post/894403/


All Articles