views:

810

answers:

3

I have created a simple Facebook application in PHP, that greets user by there user name, I also want there Email id, to be displayed. But i am not able to do that. the code that i am using is,

require_once('facebook.php');
require_once('config.php');
$facebook = new Facebook(APIKEY, SECRETKEY);
$user=$facebook->require_login();

echo $user; // displaying the ID
<div style="padding: 10px;" id="greeting">
   <fb:if-is-app-user uid="loggedinuser">
      <h2>Hi <fb:name firstnameonly="true" uid="loggedinuser" useyou="false"/>! welcome to facebook</h2>
  <fb:else>
       <h2>Hi <fb:name firstnameonly="true" uid="loggedinuser" useyou="false"/>! welcome to facebook</h2>
   </fb:else>
   </fb:if-user-has-added-app>
</div>

the Output that i am getting is,

1000002020202020
Hi User! welcome to facebook

I want the Email address to be displayed along with the user name, i searched many code but did not get any solution. and if you any good facebook tutorial site please post the links too..

+1  A: 

You need to ask for extended permissions when the user authorizes your application, so you can have access to the user email.

Here's an example using the new PHP SDK and the Graph API:

<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/config.php');

// initialize facebook
$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET));

$user = $facebook->getUser();
$session = $facebook->getSession();

// in case we don't have a valid session, we redirect asking for email extended permissions
if ($user == null || $session == null) {
  $params = array();
  $params["canvas"] = "1";
  $params["fbconnect"] = "0";
  $params["next"] = CANVAS_URL;
  $params["req_perms"] = "email";

  $loginUrl = $facebook->getLoginUrl($params);

  echo '<fb:redirect url="' . $loginUrl . '"/>';
  exit();
}

// get user email via the new graph api, using the fql.query method
$url = "https://api.facebook.com/method/fql.query";
$url .= "?access_token=" . $session['access_token'];
$url .= "&query=SELECT email FROM user WHERE uid={$user}";
$userData = simplexml_load_file($url);
$userEmail = $userData->user->email;

echo 'The user ID is: ' . $user;
echo 'The user name is: <fb:name uid="' . $user . '" />';
echo 'The user email is: ' . $userEmail;
agbb
the result i got by running the $url (in browser)..<fql_query_response list="true">−<user><email>[email protected]</email></user></fql_query_response>but $userData was getting null.. i dont know why this is happening?
Harish Kurup
maybe there's some issue with simplexml in your server, you could use $json = file_get_contents($url); instead, adding "
agbb
or maybe cURL...
agbb
ok, i will try that...but any way i am getting the Email id. Thank you @agbb
Harish Kurup
A: 

You can get the Email Address, directly without using the FQL.

// initialize facebook
 $facebook = new Facebook(array(
        'appId' => APP_ID,
        'secret' => APP_SECRET));

 $session = $facebook->getSession();
 if ($session) {
 try {
    $fbme = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
else
{
  echo "You are NOT Logged in";
}

//getting the login page if not signned in
if (!$fbme) {
  $loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
                                      'fbconnect' => 0,
                                      'req_perms' =>                   'email,user_birthday,publish_stream',
                                      'next' => CANVAS_PAGE,
                                      'cancel_url' => CANVAS_PAGE ));
 echo '<fb:redirect url="' . $loginUrl . '" />';
 } else {

      // $fbme is valid i.e. user can access our app
     echo "You can use this App";
 }

 // getting the profile data
 $user_id = $fbme[id];
 $name=$fbme['name'];
 $first_name=$fbme['first_name'];
 $last_name=$fbme['last_name'];
 $facebook_url=$fbme['link'];
 $location=$fbme['location']['name'];
 $bio_info=$fbme['bio'];
 $work_array=$fbme['work'];
 $education_array=$fbme["education"];
 $email=$fbme['email'];
 $date_of_birth=$fbme['birthday'];

This code worked for me..and i go all the information needed with the Email ID.

NOTE: User has to allow us to get their Information during the Approval of Application.
Harish Kurup