How to get user email id in facebook application using php?

I created a simple Facebook application in PHP that welcomes the user on behalf of the user. I also want the email id to be displayed there. But I can’t do it. The code I use 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 result that I get is

 1000002020202020 Hi User! welcome to facebook 

I want the email address to be displayed along with the username, I was looking for a lot of code, but didn't get any solution. and if you have a good facebook tutorial site, post links too.

+7
php facebook
source share
3 answers

You can get the email address directly without using 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 ie 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 send all the information needed with the email id.

 NOTE: User has to allow us to get their Information during the Approval of Application. 
+8
source share

You need to request extended permissions when the user authorizes your application so that you can access the user's email.

Here is an example of using the new PHP SDK and 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; 
+2
source share

Thanks for the help. I will finally figure out how to do this.

Firstly, I need to get an access token, not a user session, but one for the administrator.

So here is the code:

 $url = 'https://graph.facebook.com/oauth/access_token'; $ch = curl_init(); $params = array( 'grant_type'=>'client_credentials', 'client_id'=>$appId, 'client_secret'=>$secret, ); $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&'); // set URL and other appropriate options curl_setopt_array($ch, $opts); curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser $adminAccessToken = curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); 

And then I can use the facebook API to get any user that accepts the application and extended permission:

 $fql = "select name, hometown_location, sex, pic_square, email from user where uid=1000000001"; $param = array( 'method' => 'fql.query', 'query' => $fql, 'access_token' =>$adminAccessToken , 'callback' => '' ); $fqlResult2 = $facebook->api($param); 

with an access token, I can also send messages to the user's wall. :)

+1
source share

All Articles