How to Implement facebooklogin in php

Source: Internet
Author: User
On foreign websites, we can see that many websites have facebook logon. How can we use facebook to log on? In the previous blog, we learned how to obtain user information in js, but how to obtain it in the PHP file? First, you must create your own application. log on to www.facebook.com to register a facebook account. Second,

On foreign websites, we can see that many websites have facebook logon. How can we use facebook to log on? In the previous blog, we learned how to obtain user information in js, but how to obtain it in the PHP file? First, you must create your own application. log on to www.facebook.com to register a facebook account. Second,

On foreign websites, we can see that many websites have facebook logon. How can we use facebook to log on? In the previous blog, we learned how to obtain user information in js, but how to obtain it in the PHP file?

Step 1: Create your own application.

Log on to www.facebook.com to register a facebook account. Then, log on to www.facebook.com/developer.and register the Account as a facebookdeveloper to create an application,

The above box is displayed, where "Name" indicates the Name of your website, and "website with facebook login" indicates the redirection page after accessing facebook. sandbox mode must be disabled, otherwise, you will not be able to obtain the user information. Click Save and the following message will be displayed: Where, appid is the id of your website and will be used later, just like app secret.

Step 2. Download the sdk language package: https://github.com/facebook/php-sdk. the agent can be disabled on the terminal,

Step 3: add buttons to the attempted file. There are several buttons

(1) the button that comes with facebook. The code is This code can automatically add buttons in the form of buttons

(2) Add a link such as getLoginUrl (array ('scope '=> 'email, publish_stream');?> ">, The array ('scope '=> 'email, publish_stream') indicates that the website can obtain the user's email permission, $ facebook-> getLoginUrl (array ('scope '=> 'email, publish_stream') returns a link,

Step 4: add the Running code

Add the following code on the same page:


Script
Window. fbAsyncInit = function (){
FB. init ({
AppId: GetAppId () ;?>,
Status: true,
// Cookie: true,
Xfbml: true,
Oauth: true
});
};
(Function (d ){
Var js, id = 'Facebook-jssdk ', ref = d. getElementsByTagName ('script') [0];
If (d. getElementById (id) {return ;}
Js = d. createElement ('script'); js. id = id; js. async = true;
Js. src = "// connect.facebook.net/en_US/all.js ";
Ref. parentNode. insertBefore (js, ref );
} (Document ));
Script

Add the following code to the webpage after the website with facebook login is accessed,

Require_once 'Facebook. php'; // facebook. php is a file that has just been stored on the official website, in the src folder.

$ Facebook = new Facebook (array (
'Appid '=> appId of the newly registered website
'Secret' => appsecret of the newly registered website
));
$ User = $ facebook-> getUser ();

$ User_profile = $ facebook-> api ("/me"); // get user information

Print_r ($ user_profile );

In this way, you can get the user information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.