2013-07-24 27 views
3

我對這段代碼有一個很大的問題。我嘗試寫我的第一個Facebook應用程序。但是這個代碼不能從用戶那裏得到電子郵件,我不知道爲什麼。有人能幫我嗎?我不能在Facebook上獲得用戶的電子郵件地址

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="[email protected]"; 
     $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);  
} 
在應用程序設置,我有用戶&朋友電子郵件的權限,所以哪裏是錯誤

? 這個程序做工精細,而不範圍

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>";   
} 
+1

你看到的任何錯誤? – Lix

+0

你在哪裏定義你的應用程序的範圍? –

+0

我沒有看到任何錯誤,這是一個點,但這個程序仍然沒有得到來自Facebook用戶 – Anamesh

回答

0

你這樣做是對的,但電子郵件將只顯示誰已設置他們的隱私選項,他們的電子郵件中顯示的所有用戶,否則它不會顯示你用戶的電子郵件。

0

當且僅當用戶在驗證過程中拒絕訪問時,Facebook纔會返回用戶的電子郵件。嘗試打印請求facebook api返回的print_r($user_profile)。或者您可以使用opauth一個PHP認證庫sleeky,使用方便希望它有助於

相關問題