logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ login with facebook using php sdk

Login With Facebook Using Php Sdk

by Admin _ May 21, 2022

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

*
."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 class="https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgimg-rounded"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;# save the user nformation in session variable$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfb_id"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = $fbid."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;$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfb_name"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = $fbfullname."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;$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfb_email"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = $fbemail."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;$_SESSION<"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgfb_pic"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg> = $fbpic."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; 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();session_destroy();// redirecting user back to app login pageheader("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgLocation: ./"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg);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; else // replace your website URL same as added in the developers.Facebook.com/apps e.g. If you used http instead of https and you used $loginUrl = $helper->getLoginUrl("https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpghttps://phpstack-21306-56790-161818.darkedeneurope.comapps.com"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg, $permissions);echo "https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpgLog in with Facebook!"https://darkedeneurope.com/how-to-use-facebook-api-php/imager_7_18160_700.jpg;?>Once done, go khổng lồ the application URL in browser. You will see the Facebook button for login. Click it và the page will redirect to lớn Facebook login page. Sign into the Facebook account. After successful login, the page will be redirected lớn the profile page.

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.

Share Tweet Linkedin Pinterest
Previous Post

Hàm fgetc() trong php có chức năng gì?

Next Post

Convert string array to array php

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
cách tính năm nhuận trong php

Cách tính năm nhuận trong php

04/07/2022
bản khai đăng ký tên miền

Bản khai đăng ký tên miền

02/07/2022
phần mềm lập trình php tốt nhất

Phần mềm lập trình php tốt nhất

01/07/2022
ghi file trong php

Ghi file trong php

30/06/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Gio cam hoa

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.