Long registration forms that are a huge hassle to lớn fill & a real pain lớn validate are now a thing of the past. These days, developers have worked around this thorny UX issue by using Facebook Login for their applications.
Bạn đang xem: Login with facebook using php sdk






Go to the application thư mục by running the following command
$ cd applications/your application folder/private_html/
Download Facebook SDK
Composer is the recommended method of installing libraries on the platform and I will use it to install Facebook SDK.The first step is the creation of a composer file. In the SSH terminal run composer init command and địa chỉ the required information to it. It will create a composer.json tệp tin in the application folder. địa chỉ the following code in composer.json."https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgrequire"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg : "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook/graph-sdk"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg : "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg~5.0"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg Now run composer install command in SSH to lớn finalize installation of Facebook SDK.
How can I initialize PHP SDK on Facebook và Get User Information
It is now time to initialize Facebook SDK & get access_token from Facebook. After authenticating the token, the user info will be fetched & the view will be redirected to the profile page that shows the user information.
Xem thêm: Download Wwe 2K19 Ppsspp Android Download Now, Wwe Smackdown Vs Raw 2K14
Create two files in the application folder; Index.php và Profile.php.
Add the following code khổng lồ index.php file. Comments are added to lớn explain the code.
"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg1214799411888113"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg, "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgapp_secret"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg => "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg286f08b36691768f859a70e788f4ceda"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg, "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgdefault_graph_version"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg => "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgv2.5"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg,>);$helper = $fb->getRedirectLoginHelper();$permissions = <"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgemail"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>; // optionaltry if (isset($_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>)) $accessToken = $_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>; else $accessToken = $helper->getAccessToken(); catch(FacebookExceptionsfacebookResponseException $e) // When Graph returns an errorecho "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgGraph returned an error: "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg . $e->getMessage(); exit; catch(FacebookExceptionsFacebookSDKException $e) // When validation fails or other local issuesecho "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgFacebook SDK returned an error: "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg . $e->getMessage(); exit;if (isset($accessToken)) if (isset($_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>)) $fb->setDefaultAccessToken($_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>); else // getting short-lived access token$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = (string) $accessToken; // OAuth 2.0 client handler$oAuth2Client = $fb->getOAuth2Client();// Exchanges a short-lived access token for a long-lived one$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>);$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = (string) $longLivedAccessToken;// setting mặc định access token khổng lồ be used in script$fb->setDefaultAccessToken($_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfacebook_access_token"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>);// redirect the user to the profile page if it has "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgcode"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg GET variableif (isset($_GET<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgcode"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg>)) header("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgLocation: profile.php"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg);// getting basic info about usertry $profile_request = $fb->get("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg/me?fields=name,first_name,last_name,email"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg);$requestPicture = $fb->get("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg/me/picture?redirect=false&height=200"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg); //getting user picture$picture = $requestPicture->getGraphUser();$profile = $profile_request->getGraphUser();$fbid = $profile->getProperty("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgid"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg); // to Get Facebook ID$fbfullname = $profile->getProperty("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgname"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg); // to lớn Get Facebook full name$fbemail = $profile->getProperty("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgemail"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg); // khổng lồ Get Facebook email$fbpic = "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg

Add the following code to lớn profile.php khổng lồ show the retrieved information. For the purpose of this tutorial, I will retrieve user id, Facebook profile picture, name và email.