How to get Facebook user email address using graphical API?

I read this post again and again, and yet it does not make sense to me.

What I'm trying to do is get the email address of a Facebook user so that I can store it in a string and compare to find out if it matches the addresses of existing users in my MySQL database. I only need to do this when the user first grants access rights to the application.

In the "Verified referrals" section of my application developer interface, I set "Permissions for users and friends" to enable "email."

In the canvas area of ​​my site, I have this code:

include('facebook/base_facebook.php'); include('facebook/facebook.php'); $facebook = new Facebook(array( 'appId' => "XXXXXXXXXXXXXXX", 'secret' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", )); $user = $facebook->getUser(); if ($user) { try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { $user = null; } } 

If I var_dup () $ user, I can see the user ID (albeit a little more), which seems to be correct, so I believe that I am connecting to the right place on Facebook.

But then there was this suggested line of code , which means that it returns the email address, but when I repeat it to see how it looks, it does not return the email address:

 echo "<br/>" . $facebook->getLoginUrl(array('req_perms' => 'email')); 

I thought that this was not so, because it is not quite right for me, but, on the other hand, I just have no idea what else to do with this or where to put it.

And then I also came across this post which has code mixed with HTML in a way that puzzles me even more.

Assuming my user allowed my application to see their email, is there an easy way to get it? Something like an inexperienced programmer like me can understand?

+4
source share
3 answers

You are using an outdated method. You can use the code below to receive an email.

Further information on Facebook Connect can be found at: http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-xx-which-uses-graph-api /

 $app_id = "xxxxxxxxx"; $app_secret = "xxxxxxxxxx"; $site_url = "xxxxxxxxxxxxx"; include_once "src/facebook.php"; $facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, )); $user = $facebook->getUser(); if($user){ try{ $user_profile = $facebook->api('/me'); }catch(FacebookApiException $e){ $user = NULL; } } if($user){ $logoutUrl = $facebook->getLogoutUrl(); }else{ $loginUrl = $facebook->getLoginUrl(array( 'scope' => 'email', 'redirect_uri' => $site_url, )); } if($user){ Echo "Email : " . $user_profile['email']; } 
+4
source

As pointed out in the comments, you should replace req_perms with scope , as this is the name for the field indicating what permissions will be granted to users.

Once email permission is granted (do you see it in the Auth dialog box?), You can get the email field through the Graph API. You can also get only the required fields in the results by specifying "em" in the fields parameter:

 $facebook->api('/me', array('fields' => 'id,email')); 

BTW . Please do not pay attention to the fact that both posts to which you refer are old (2010), and much more has changed on the platform. Be sure to read the updated Permissions and Authentication documentation.

+4
source

You can associate your Facebook username with an email address. For example, my username is umair.hamid.79 on Facebook. This will be " umair.hamid.79@facebook.com ".

0
source

Source: https://habr.com/ru/post/1402199/


All Articles