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';
$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
{
$user_profile = $facebook->api('/me');
echo "Name: " . $user_profile['name'];
$_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)
{
$login_url = $facebook->getLoginUrl();
error_log($e->getType());
error_log($e->getMessage());
}
?>
//jQuery Mobile Form would be here
<?php else: ?>
<?php
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => "http://localhost/share/#postAJourney",
'display' => 'popup',
));
?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $login_url; ?>">Login with Facebook</a>
</div>
<?php endif ?>
I'm not getting user details at all. After I login and I am shown the form, the url contains more data but not sure what to do with that. I tried the example from the link I provided and I have the same issue. –