如何使用PHP在Facebook应用程序中获取用户电子邮件ID? [英] How to get Users Email Id in Facebook application using PHP?

查看:142
本文介绍了如何使用PHP在Facebook应用程序中获取用户电子邮件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在PHP中创建了一个简单的Facebook应用程序,通过用户名,
向用户打招呼,我也希望显示电子邮件ID。但我不能这样做。我使用的代码是

  require_once('facebook.php'); 
require_once('config.php');
$ facebook = new Facebook(APIKEY,SECRETKEY);
$ user = $ facebook-> require_login();

echo $ user; //显示ID
< div style =padding:10px; ID = 问候 >
< fb:if-is-app-user uid =loggedinuser>
< h2> Hi< fb:name firstnameonly =trueuid =loggedinuseruseyou =false/>!欢迎来到facebook< / h2>
< fb:else>
< h2> Hi< fb:name firstnameonly =trueuid =loggedinuseruseyou =false/>!欢迎来到facebook< / h2>
< / fb:else>
< / fb:if-user-has-added-app>
< / div>

我得到的输出是

  1000002020202020 
嗨用户!欢迎来到Facebook

我希望电子邮件地址与用户名一起显示,我搜索了很多代码但没有得到任何解决方案。如果您有任何好的Facebook教程网站,请张贴链接。

解决方案

您可以直接获取电子邮件地址,而无需使用FQL。

  // initialize facebook 
$ facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET));

$ session = $ facebook-> getSession();
if($ session){
try {
$ fbme = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
error_log($ e);
}
}
else
{
echo你没有登录;
}

//如果没有在
中签名,获取登录页面if(!$ fbme){
$ loginUrl = $ facebook-> getLoginUrl(array 'canvas'=> 1,
'fbconnect'=> 0,
'req_perms'=>'email,user_birthday,publish_stream',
'next'=> CANVAS_PAGE,
'cancel_url'=> CANVAS_PAGE));
echo'< fb:redirect url ='。$ loginUrl。'/>';
} else {

// $ fbme是有效的,即用户可以访问我们的应用程序
echo您可以使用此App;
}

//获取个人资料
$ user_id = $ fbme [id];
$ name = $ fbme ['name'];
$ first_name = $ fbme ['first_name'];
$ last_name = $ fbme ['last_name'];
$ facebook_url = $ fbme ['link'];
$ location = $ fbme ['location'] ['name'];
$ bio_info = $ fbme ['bio'];
$ work_array = $ fbme ['work'];
$ education_array = $ fbme [education];
$ email = $ fbme ['email'];
$ date_of_birth = $ fbme ['birthday'];

此代码为我工作,并且我将使用电子邮件ID所需的所有信息。 p>

 注意:用户必须允许我们在批准申请时获取他们的信息。 


I have created a simple Facebook application in PHP, that greets user by there user name, I also want there Email id, to be displayed. But i am not able to do that. the code that i am using is,

require_once('facebook.php');
require_once('config.php');
$facebook = new Facebook(APIKEY, SECRETKEY);
$user=$facebook->require_login();

echo $user; // displaying the ID
<div style="padding: 10px;" id="greeting">
   <fb:if-is-app-user uid="loggedinuser">
      <h2>Hi <fb:name firstnameonly="true" uid="loggedinuser" useyou="false"/>! welcome to facebook</h2>
  <fb:else>
       <h2>Hi <fb:name firstnameonly="true" uid="loggedinuser" useyou="false"/>! welcome to facebook</h2>
   </fb:else>
   </fb:if-user-has-added-app>
</div>

the Output that i am getting is,

1000002020202020
Hi User! welcome to facebook

I want the Email address to be displayed along with the user name, i searched many code but did not get any solution. and if you any good facebook tutorial site please post the links too..

解决方案

You can get the Email Address, directly without using the FQL.

// initialize facebook
 $facebook = new Facebook(array(
        'appId' => APP_ID,
        'secret' => APP_SECRET));

 $session = $facebook->getSession();
 if ($session) {
 try {
    $fbme = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
else
{
  echo "You are NOT Logged in";
}

//getting the login page if not signned in
if (!$fbme) {
  $loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
                                      'fbconnect' => 0,
                                      'req_perms' =>                   'email,user_birthday,publish_stream',
                                      'next' => CANVAS_PAGE,
                                      'cancel_url' => CANVAS_PAGE ));
 echo '<fb:redirect url="' . $loginUrl . '" />';
 } else {

      // $fbme is valid i.e. user can access our app
     echo "You can use this App";
 }

 // getting the profile data
 $user_id = $fbme[id];
 $name=$fbme['name'];
 $first_name=$fbme['first_name'];
 $last_name=$fbme['last_name'];
 $facebook_url=$fbme['link'];
 $location=$fbme['location']['name'];
 $bio_info=$fbme['bio'];
 $work_array=$fbme['work'];
 $education_array=$fbme["education"];
 $email=$fbme['email'];
 $date_of_birth=$fbme['birthday'];

This code worked for me..and i go all the information needed with the Email ID.

NOTE: User has to allow us to get their Information during the Approval of Application.

这篇关于如何使用PHP在Facebook应用程序中获取用户电子邮件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆