3

i have a big problem with this code. I try to write my first facebook app. But this code cant get email from user, and i dont know why. Can someone help me?

require_once 'phpmailer.php';
require_once 'facebook.php';

error_reporting(0);
//Application Configurations
$app_id     = 'MY APP ID';
$app_secret = 'MY SECRET ID';
$site_url   = 'MY SITE URL';
// Create our application instance
$facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    'cookie'=>true,
));

$user = $facebook->getUser();

if($user == 0){
    $user = $facebook->getUser();
}
if($user){
    $user_profile=$facebook->api('/me');    
    $logoutUrl= $facebook->getLogoutUrl();
    if(empty($_POST['send'])){
        echo"<form method='POST'>";
        echo"Hi ".$user_profile['name']."</br>";

        echo"<textarea name='message' rows='6' cols='80'>";
        echo"</textarea>";
        echo"<input type='submit' name='send' value='send'>";
        echo"</form>";
    }
    if(!empty($_POST['send'])&& empty($_POST['message'])){
        echo "Empty message";
    }
    if(!empty($_POST['send'])){
        $mail = new PHPMailer();
        $mail->SetFrom($_POST['mail'],$user_profile['name']);
        $mail->CharSet="utf-8";
        $address="therimsilua@gmail.com";
        $mail->AddAddress($address," ");
        $mail->Subject="message from facebook";
        $wiadomosc='Message from user'.$user_profile['name'].'</br>'.$_POST['message'];
        $mail->MsgHTML($message); 
        if($mail->Send()){
            echo "Send to: ".$user_profile['email']; 
        }
        else{           
            echo "Error:".$mail->ErrorInfo;
        }
    }               
}
else {
    $perm = array('scope'=>'email');
    $loginUrl =$facebook->getLoginUrl($perm);       
}

in App setting i have User & Friend Permissions email , so where is the error ? This app work fine without scope

require_once "facebook.php";

$facebook = new Facebook(array('appId'  => '482455065177441', 'secret' => '0e484981225df74f1170c29185aa8690'));  

$user_fb = $facebook->getUser();

if($user_fb == 0)
{
    $user_fb = $facebook->getUser();
}

if ($user_fb) // Check user's FB user ID has getting or not
{   
$user_profile = $facebook->api('/me'); 

$logoutUrl = $facebook->getLogoutUrl(); 

echo $user_profile['email'];
echo $user_profile['first_name'];
echo $user_profile['last_name'];            
}

else // user's FB user ID has not getting load login url with email permission
{     
 $perms = array('scope' => 'email');
 $loginUrl = $facebook->getLoginUrl($perms);        
 echo "<script> top.location.href='" . $loginUrl . "'</script>";        
}
Anamesh
  • 73
  • 2
  • 11
  • 1
    Are you seeing any errors? – Lix Jul 24 '13 at 08:33
  • Where are you defining scope for your app ?? – Jay Hardia Jul 24 '13 at 08:35
  • i dont see any errors and this is a point, but this app still dont get a email from facebook user – Anamesh Jul 24 '13 at 08:36
  • Jay Harida i try other test app , who get me just a user email, and its work fine without scope – Anamesh Jul 24 '13 at 08:38
  • In this case , please check your app, you have to define permission on your app for getting email. try this. – Jay Hardia Jul 24 '13 at 08:39
  • You mean permission in app setting ? or still scope in this code? – Anamesh Jul 24 '13 at 08:43
  • firstly,permission in app setting and try . If not working then scope in code. – Jay Hardia Jul 24 '13 at 08:44
  • check answer here, i hope this will help you, http://stackoverflow.com/questions/9767949/how-to-get-the-email-address-of-a-facebook-user-using-the-graph-api?rq=1 – Jothi Kannan Jul 24 '13 at 08:45
  • i have set permision in app setting, so i try with scope, or jothikannan solution – Anamesh Jul 24 '13 at 08:49
  • I use this http://stackoverflow.com/questions/9767949/how-to-get-the-email-address-of-a-facebook-user-using-the-graph-api?rq=1 and its still dont work,(but again if i use just code which show me a user email, its work OK) like i said before i have set user and friend permission: email , need to do something else? – Anamesh Jul 24 '13 at 09:25
  • check this post, it should help you: http://stackoverflow.com/questions/17808974/not-getting-email-from-facebook-api-while-login/17809808?noredirect=1#comment26020437_17809808 – Amjo Jul 24 '13 at 09:49
  • have tried removing the app from your facebook account? And then give permission to it again? Or try with other account? What seems to happen is that you're still using an old access token – Fabio Antunes Jul 24 '13 at 11:07

2 Answers2

1

You are doing it right, but the email will only be displayed for the users who have set their privacy options for their emails to be shown to all, otherwise it wont display you the email of the user.

0

Facebook would not return the user's email if and only if the user denies u access during authentication. Try to print print_r($user_profile) out the request facebook api returns. or you can use opauth a php authentication library sleeky and easy to use hope it helps