login.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://www.123.com/login-callback.php', $permissions);
echo '<a href="' . htmlspecialchars($loginUrl) . '">Login via Facebook!</a>';
login-callback.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new \Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $accessToken );
// $response = $fb->get('/me', $accessToken );
}
catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
When I click on the "login.php" output link, it jumps to the "login-callback.php" and prints the following error message. How do I solve this problem?
Graph returned an error: Unable to load URL: The domain name of this URL does not contain the domain name of the application. In order to be able to load the URL, add all domains and subdomains of the application in the App Domain Name field of the app settings.
However, I have added the corresponding url in the facebook developer platform.
Now OAuth is mandatory in strict mode for FB Login: You have to set the URI of your callback script in the params of FB Login in your app settings board, AND pass the same callback URL as an argument of getAccessToken() method:
try {
$accessToken = $helper->getAccessToken('http://yourdomain.com/login-callback.php');
}// ...