facebook - FB: PHP simple connect script : how to get email (PHP SDK OAuth 2.0 )? -


i using simple php connect script:

http://code.runnable.com/ufwdes1fqz9uaaah/simple-facebook-connect-php-example

i retrieve name, id

how can retrieve email ?

i tried

 $user_profile = $facebook->api('/me?fields=email');  $user_profile = $facebook->api('/me/email'); 

with no success !

<?php  require 'lib/facebook-php-sdk/src/facebook.php';  // create our application instance (replace appid , secret). $facebook = new facebook(array(   'appid'  => '',   'secret' => '', ));  // user id $user = $facebook->getuser();  // may or may not have data based on whether user logged in. // // if have $user id here, means know user logged // facebook, don't know if access token valid. access // token invalid if user logged out of facebook.  if ($user) {   try {     // proceed knowing have logged in user who's authenticated.     $user_profile = $facebook->api('/me');   } catch (facebookapiexception $e) {     error_log($e);     $user = null;   } }  // login or logout url needed depending on current user state. if ($user) {   $logouturl = $facebook->getlogouturl(); } else {   $loginurl = $facebook->getloginurl(); }   ?> <!doctype html> <html>   <body>       <?php if ($user): ?>       <a href="<?php echo $logouturl; ?>">logout</a>     <?php else: ?>       <div>         login using oauth 2.0 handled php sdk:         <a href="<?php echo $loginurl; ?>">login facebook</a>       </div>     <?php endif ?>      <h3>php session</h3>     <pre><?php print_r($_session); ?></pre>      <?php if ($user): ?>       <h3>you</h3>       <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">        <h3>your user object (/me)</h3>       <pre><?php print_r($user_profile); ?></pre>     <?php else: ?>       <strong><em>you not connected.</em></strong>     <?php endif ?>     </body> </html> 

replace $loginurl var with

$facebook->getloginurl(array("scope" => "email")); 

afterwards can use

/me?fields=email 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

node.js - Express and Redis - If session exists for this user, don't allow access -

excel - I can't get the attachement of the email PHP -