3

我对这段代码有一个大问题。我尝试编写我的第一个 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="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);       
}

在应用程序设置中我有用户和朋友权限电子邮件,那么错误在哪里?这个应用程序在没有范围的情况下工作正常

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>";        
}
4

2 回答 2

1

你做对了,但是电子邮件只会显示给那些设置了他们的电子邮件的隐私选项的用户,否则它不会向你显示用户的电子邮件。

于 2013-07-31T09:07:19.580 回答
0

当且仅当用户在身份验证期间拒绝您访问时,Facebook 才会返回用户的电子邮件。尝试打印print_r($user_profile)出 facebook api 返回的请求。或者您可以使用 opauth一个 php 身份验证库,时尚且易于使用,希望对您有所帮助

于 2013-07-31T16:18:06.920 回答