I am using Facebook javascript sdk and php sdk to login user to my web app. With javascript I have the login/out working and I can get a logged in user's public facebook details.
However, when using php, I am not getting the user's facebook details. To give you some info on the app, when I navigate to a certain page that contains a form, the code checks to see if their logged in. If not, they are given a LoginUrl link. Once logged in, I have the $user_id, so now the user can use the form, but I am not getting their facebook details to save in MySQL database when form is submitted. Does anyone have advice to what I should do? I definitely have the $user_id as the form is only shown if I have the $user_id.
I based my code on https://developers.facebook.com/docs/reference/php/facebook-api/
<?php
require 'facebook.php';
//create application instance
$config = array(
'appId' => '**************',
'secret' => '****************************',
'cookie' => true);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
//Page containing form
<div data-role="page" id="postAJourney" data-add-back-btn="true" data-back-btn-text="back" data-rel="back" data-direction="reverse" data-transition="flip" data-theme="b" >
<div data-role="header" data-position="fixed">
<h1>Post A Journey</h1>
</div>
<div data-role="content">
<?php if ($user_id): ?>
<form action="add_journey.php" method="post" id="postAJourneyForm">
<?php
try
{
$access_token = $facebook->getAccessToken();
$param_token = array('access_token' => $access_token);
//Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me','GET',$param_token);
echo "Name: " . $user_profile['name'];
// store session data
$_SESSION['fb_id'] = $user_profile['id'];
$_SESSION['fb_name'] = $user_profile['name'];
$_SESSION['fb_first_name'] = $user_profile['first_name'];
$_SESSION['fb_last_name'] = $user_profile['last_name'];
$_SESSION['fbImg'] = "https://graph.facebook.com/".$user_profile['id']."/picture";
$_SESSION['fbLink'] = "https://www.facebook.com/".$user_profile['name'];
}
catch(FacebookApiException $e)
{
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
error_log($e->getType());
error_log($e->getMessage());
}
?>
//jQuery Mobile Form would be here
<?php else: ?>
<?php // No user, print a link for the user to login
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => "http://localhost/share/#postAJourney"
));
?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $login_url; ?>">Login with Facebook</a>
</div>
<?php endif ?>