Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
<?php
require 'facebook.php';
//create application instance
$config = array(
'appId' => '**************',
'secret' => '****************************',
'cookie' => true);
$facebook = new Facebook($config);
$user_id = $facebook->getUser(); 
?>


HTML
//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
       {
       //Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me');
       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",
     '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. –
Posted
Updated 2-Apr-13 23:39pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900