{"id":2879,"date":"2015-12-02T12:21:02","date_gmt":"2015-12-02T12:21:02","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=2879"},"modified":"2016-10-31T09:29:20","modified_gmt":"2016-10-31T09:29:20","slug":"how-to-login-with-facebook-api-sdk-v5-in-php","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-login-with-facebook-api-sdk-v5-in-php_2879.html","title":{"rendered":"How to Login with Facebook API SDK v5 in PHP"},"content":{"rendered":"

Previously we wrote many article on Facebook login<\/strong> and now they are not working due to updates in Facebook API<\/strong> so I\u00a0am writing on latest SDK v5 how to login with Facebook using PHP<\/strong> SDK v5 which is the latest version and I\u00a0suggest you all to update your old Facebook login integration to the latest version.\"How<\/p>\n

[wpdm_file id=156]DEMO<\/a><\/div>\n

Let’s Start<\/strong><\/p>\n

Make New Application on Facebook:<\/p>\n

1. Visit https:\/\/developers.facebook.com\/apps<\/a> and click + Create New App<\/em>.<\/p>\n

\"create<\/p>\n

Click on website<\/em>.<\/p>\n

\"create<\/p>\n

Add application name and click Create New Facebook App ID<\/em>.<\/p>\n

\"create<\/p>\n

Select category and click on Create App ID<\/strong>. On next window click\u00a0Skip Quick Start<\/strong>.<\/p>\n

It will take you to the app dashboard now select settings and click on +Add Platform<\/strong><\/em>. Select website in add platform popup.<\/p>\n

\"create<\/p>\n

Add your website link and fill form and save like below image.<\/p>\n

\"create<\/p>\n

By default your app enabled for sandbox mode you need to change it to live click on Status & Review<\/strong>.<\/p>\n

\"create<\/p>\n

That’s it App settings done now come to the coding.<\/p>\n

First of all download SDK from here<\/a> or you can download our demo code which contain SDK as well.<\/p>\n

Now create a file index.php<\/strong> and add a facebook login button link to fblogin.php<\/strong> page and create a callback.php page to show information after authenticate.<\/p>\n

index.php<\/strong><\/p>\n

<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>Login With Facebook<\/title>\r\n<\/head>\r\n<body>\r\n  <a href=\"fblogin.php\"><img src=\"login-button.png\" alt=\"Facebook Login Button\"><\/a>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n

It will show a login with Facebook image.<\/p>\n

fblogin.php<\/strong><\/p>\n

<?php\r\nsession_start();\r\nrequire_once( 'Facebook\/autoload.php' );\r\n\r\n$fb = new Facebook\\Facebook([\r\n  'app_id' => 'ENTER-YOUR-APP-ID',\r\n  'app_secret' => 'ENTER-YOUR-APP-SECRET',\r\n  'default_graph_version' => 'v2.5',\r\n]);\r\n\r\n$helper = $fb->getRedirectLoginHelper();\r\n\r\n$permissions = ['email']; \/\/ Optional permissions for more permission you need to send your application for review\r\n$loginUrl = $helper->getLoginUrl('http:\/\/demo.phpgang.com\/facebook-login-sdk-v5\/callback.php', $permissions);\r\nheader(\"location: \".$loginUrl);\r\n\r\n?><\/pre>\n

This page verify your Facebook application credentials and redirect you to facebook.com for login. You need to change your application id<\/strong> and application secret<\/strong>.<\/p>\n

Once you allow all permission it will redirect to callback.php page and show your user’s data.<\/p>\n

callback.php<\/strong><\/p>\n

<?php\r\nsession_start();\r\nrequire_once( 'Facebook\/autoload.php' );\r\nrequire_once( 'db.php' );\r\n\r\n$fb = new Facebook\\Facebook([\r\n  'app_id' => 'ENTER-YOUR-APP-ID',\r\n  'app_secret' => 'ENTER-YOUR-APP-SECRET',\r\n  'default_graph_version' => 'v2.5',\r\n]);  \r\n  \r\n$helper = $fb->getRedirectLoginHelper();  \r\n  \r\ntry {  \r\n  $accessToken = $helper->getAccessToken();  \r\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {  \r\n  \/\/ When Graph returns an error  \r\n  \r\n  echo 'Graph returned an error: ' . $e->getMessage();  \r\n  exit;  \r\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {  \r\n  \/\/ When validation fails or other local issues  \r\n\r\n  echo 'Facebook SDK returned an error: ' . $e->getMessage();  \r\n  exit;  \r\n}  \r\n\r\n\r\ntry {\r\n  \/\/ Get the Facebook\\GraphNodes\\GraphUser object for the current user.\r\n  \/\/ If you provided a 'default_access_token', the '{access-token}' is optional.\r\n  $response = $fb->get('\/me?fields=id,name,email,first_name,last_name', $accessToken->getValue());\r\n\/\/  print_r($response);\r\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\r\n  \/\/ When Graph returns an error\r\n  echo 'ERROR: Graph ' . $e->getMessage();\r\n  exit;\r\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\r\n  \/\/ When validation fails or other local issues\r\n  echo 'ERROR: validation fails ' . $e->getMessage();\r\n  exit;\r\n}\r\n$me = $response->getGraphUser();\r\n\/\/print_r($me);\r\necho \"Full Name: \".$me->getProperty('name').\"<br>\";\r\necho \"First Name: \".$me->getProperty('first_name').\"<br>\";\r\necho \"Last Name: \".$me->getProperty('last_name').\"<br>\";\r\necho \"Email: \".$me->getProperty('email');\r\necho \"Facebook ID: <a href='https:\/\/www.facebook.com\/\".$me->getProperty('id').\"' target='_blank'>\".$me->getProperty('id').\"<\/a>\";\r\n?><\/pre>\n

That’s it for codding end now you can integrate it and run latest version of Facebook login.<\/p>\n

Note:<\/strong> This version of the Facebook SDK for PHP requires PHP 5.5 or greater.<\/p><\/blockquote>\n

Facebook<\/a><\/blockquote><\/div><\/div>
Tutorial Categories:<\/strong>
\n \"PHP\"<\/a><\/div>
\n \"Social\"<\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"

Previously we wrote many article on Facebook login and now they are not working due to updates in Facebook API so I\u00a0am writing on latest SDK v5 how to login with Facebook using PHP SDK v5 which is the latest version and I\u00a0suggest you all to update your old Facebook login integration to the latest version.\"How<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"summary_large_image","cardImageID":0,"cardImage":"https:\/\/images.phpgang.com\/2015\/12\/How-to-Login-with-Facebook-API-SDK-v5-in-PHP.png","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":"","footnotes":""},"categories":[3,255],"tags":[461,121,623],"_links":{"self":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/2879"}],"collection":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/comments?post=2879"}],"version-history":[{"count":0,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/2879\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media?parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/categories?post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/tags?post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}