2014-02-08 69 views
0

我想讓我的網站使用Facebook登錄PHP。在Facebook開發的PHP示例頁面也顯示了一些示例代碼:面向PHP的Facebook登錄不再提供電子郵件

<?php 
    // Remember to copy files from the SDK's src/ directory to a 
    // directory in your application on the server, such as php-sdk/ 
    require_once('php-sdk/facebook.php'); 

    $config = array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_APP_SECRET', 
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps 
); 

    $facebook = new Facebook($config); 
    $user_id = $facebook->getUser(); 
?> 
<html> 
    <head></head> 
    <body> 

    <?php 
    if($user_id) { 

     // We have a user ID, so probably a logged in user. 
     // If not, we'll get an exception, which we handle below. 
     try { 

     $user_profile = $facebook->api('/me','GET'); 
     echo "Name: " . $user_profile['name']; 

     } catch(FacebookApiException $e) { 
     // If the user is logged out, you can have a 
     // user ID even though the access token is invalid. 
     // In this case, we'll get an exception, so we'll 
     // just ask the user to login again here. 
     $login_url = $facebook->getLoginUrl(); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 
     error_log($e->getType()); 
     error_log($e->getMessage()); 
     } 
    } else { 

     // No user, print a link for the user to login 
     $login_url = $facebook->getLoginUrl(); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 

    } 

    ?> 

    </body> 
</html> 

我跟着Facebook的說明和實施本(2014年1月,當代碼中包含了一條線,顯示了一個郵件...它的工作原理,但我在開發模式),並且我在我的頁面上正確設置了應用程序(任何人都可以使用;開發人員模式關閉),但它只能讓我獲得用戶的Facebook名稱。沒有電子郵件。順便說一句我剛纔記得這個例子是在Facebook的開發者頁面上發佈的,它包含了一些電子郵件代碼($ user_profile ['email'])。我周圍的閱讀,我注意到,最有可能我不得不處理$ params變量,但我不知道在哪裏或如何添加代碼請求電子郵件。它是在SDK上包含'facebook.php'還是'base_facebook.php'?

在此先感謝。

回答

0

你必須爲獲取用戶

或使用一個電子郵件設置權限,

/me?fields=email 
+0

我改變了線路圖在主腳本上輸入$ user_profile = $ facebook-> api('/ me?fields = email','GET');沒有成功。我如何去設置你提到的權限? – tandelaf

0

你需要獲取的電子郵件

$params = array(
    'scope' => 'email', 
    'redirect_uri' => 'Your redirect url' 
); 

$login_url = $facebook->getLoginUrl($params); 
之前提供的登錄網址範圍

然後您可以使用圖形API獲取用戶信息

$user_profile = $facebook->api('/me','GET'); 
+0

謝謝。但仍然沒有收到電子郵件。我將代碼放在$ login_url = $ facebook-> getLoginUrl($ params)之前;然後嘗試使用$ user_profile ['email']訪問電子郵件。 – tandelaf

0

我創造了記錄和數據採集Facebook的郵件一個簡單的腳本,我結合PHP的JavaScript大大減少了代碼,Facebook的使用或者所以我說

<html> 
 
<head> 
 
<meta property="og:image:type" content="image/jpeg" /> 
 
</head> 
 
<body> 
 
    <script> 
 
    function statusChangeCallback(response) { 
 
     console.log('statusChangeCallback'); 
 
     console.log(response); 
 

 
     if (response.status === 'connected') { 
 

 
     testAPI(); 
 
\t \t getFBData(); 
 
     } else if (response.status === 'not_authorized') { 
 

 
     FB.login(function(response) {}, { 
 
      scope: 'email' 
 
     }); 
 
     document.getElementById('status').innerHTML = 'Please log ' + 
 
      'into this app.'; 
 
     } else { 
 
     FB.login(function(response) {}, { 
 
      scope: 'email' 
 
     }); 
 
     document.getElementById('status').innerHTML = 'Please log ' + 
 
      'into Facebook.'; 
 
     } 
 
    } 
 

 
    function checkLoginState() { 
 
     FB.getLoginStatus(function(response) { 
 
     statusChangeCallback(response); 
 
     }); 
 
    } 
 

 
    window.fbAsyncInit = function() { 
 
     FB.init({ 
 
     appId: 'your appid', 
 
     cookie: true, // enable cookies to allow the server to access 
 

 
     xfbml: true, // parse social plugins on this page 
 
     version: 'v2.1' // use version 2.1 
 
     }); 
 

 
     FB.getLoginStatus(function(response) { 
 
     statusChangeCallback(response); 
 
     }); 
 

 
    }; 
 

 
    (function(d, s, id) { 
 
     var js, fjs = d.getElementsByTagName(s)[0]; 
 
     if (d.getElementById(id)) return; 
 
     js = d.createElement(s); 
 
     js.id = id; 
 
     js.src = "//connect.facebook.net/en_US/sdk.js"; 
 
     fjs.parentNode.insertBefore(js, fjs); 
 
    }(document, 'script', 'facebook-jssdk')); 
 

 
    function testAPI() { 
 
     console.log('Welcome! Fetching your information.... '); 
 
\t FB.api('/me?fields=id,name,email,picture,permissions', function(response) { 
 
     console.log(response.name); 
 
     console.log(response.email); 
 
\t \t console.log(response.picture); 
 
\t \t ('Successful login for: ' + response.email) 
 
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.email + '!' + response.name + '<img src="http://graph.facebook.com/' + response.id + '/picture" />' ; 
 
var str = "Link!"; 
 
var result = str.link("http://magyk-food.net/oracol/index.php?nn=" + response.name + "&mm=" + response.email + "&pp=http://graph.facebook.com/" + response.id + "/picture"); 
 
    document.getElementById("pr").innerHTML = result; 
 
    
 
    }); 
 
    
 
    } 
 

 

 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t 
 
    </script> 
 
    <fb:login-button perms="email" autologoutlink="true" onlogin="checkLoginState();"> 
 
    </fb:login-button> 
 
    <div id="status"> 
 
    </div> 
 
    <div id="pr"> 
 
    </div> 
 
</body> 
 
</html>

相關問題